Showing posts with label spring 3. Show all posts
Showing posts with label spring 3. Show all posts

29 July 2011

How to add and configure log4j.XML file in Spring 3

Solution works for Spring 3.0.5 and log4j  1.2.16 (but it should works with previous/future releases of both programs)

It is really easy task:

I assumed that you already has spring 3 stuff in place.
  1. You need add log4j to your libraries in your project
  2. Add information about log4j to web.xml file (It should be /MyCozyProject/src/main/webapp/WEB-INF/web.xml  
  3. Add information about location for log4j.xml  in context.xml
  4. Configure your log4j 
Information which you need add in .2 (It tells spring where to look for information about log4j)
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

In 3. in context.xml (usually should be /src/main/webapp/META-INF/context.xml ) add information about where your log4j.xml file is
<Context displayName=" MyCozyProject ">
    <Parameter name="log4jConfigLocation" value="file:C:\myCozyProject\log4j.xml"/>
</Context>

In 4 You just configure your log4j to your needs (below is example of saving log to file)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="tofile" class="org.apache.log4j.FileAppender">
  <param name="file" value="C:\\myCozyProject.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/>
</layout>
</appender>

<root>
<priority value ="info" />
<appender-ref ref="tofile" />
</root>

</log4j:configuration>


 btw. If you want log4j.xml inside project, then you can use my example or check this blog :
http://th1rty7.blogspot.com/2009/03/configure-logging-in-spring-with-log4j.html

28 July 2011

How to inject parameter to controller in spring 3?

Solution for Spring 3 (Annotations)

In many tutorials you can find how to add parameter to service and it looks is , it is and ...  it doesn't work when you trying apply "easy successful receipe" to controller, so shit hit the fun and you feel screwed up.

However... solution for your problem is still simply.
You need:
1)Set parameter in META-INF/context.xml
 <?xml version="1.0" encoding="UTF-8"?>
 <Context displayName="Another awsome program">
 <Parameter name=" param/naughty/url " value="http://pastor.ovh.org"/>
 (... bunch of other stuff ...) 
</Context> 
2) In your marvellous controller
package org.ovh.pastor.mvc.controller; 
 @Controller
public classAwsomeController  {
(  ... bunch of other stuff ...) 
private @Value("${param/naughty/url}") String doggyWebsite;  
 public void setDoggyWebsite ( String doggyWebsite ) {
this. doggyWebsite = doggyWebsite ;
 } 
(  ... bunch of other stuff ...)   
}

 Little explanation of most important line:

  private @Value("${param/naughty/url}") String doggyWebsite;

where :
  •  private  - modifier
  • @value - is a lovely annotation that contain information where is parameter 
  • String -  data type 
  •  doggyWebsite  - vairable name
     

Hope , it helps . Keep smiling and pray that someday spring will be easier , better explained by examples.