Showing posts with label Java Spring. Show all posts
Showing posts with label Java Spring. Show all posts

Wednesday, April 10, 2013

Spring Note

Spring 


1. Annotation injection is performed before XML injection, thus the latter configuration will override the former for properties wired through both approaches.


Annotation wiring is not turned on in the Spring container by default. So, before we can use annotation-based wiring, we will need to enable it in our Spring configuration file. So consider to have following configuration file in case you want to use any annotation in your Spring application.
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:annotation-config/>
   <!-- bean definitions go here -->

</beans>
Once <context:annotation-config/> is configured, you can start annotating your code to indicate that Spring should automatically wire values into properties, methods, and constructors. Let us see few important annotations to understand how they work:

Thursday, April 4, 2013

Maven-Spring

OPTION1

1. install m2Eclipse in Eclipse
2. Create maven project
3. create SOURCE FOLDER -> "sre/main/java/resources"
4. add config xml files to resources folder
5. add dependencies to pom.xml

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
</dependencies>
...

Maven will download those dependencies automatically.




OPTION2

In mkyong's example, he established the maven project in linux and then import it into eclipse.
http://www.mkyong.com/spring/quick-start-maven-spring-example/

1. Generate project structure with Maven

In command prompt, issue following Maven command :
mvn archetype:generate -DgroupId=com.mkyong.common -DartifactId=SpringExamples 
 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Maven will generate all the Java’s standard folders structure for you (besides resources folder, which you need to create it manually)

2. Convert to Eclipse project

Type “mvn eclipse:eclipse” to convert the newly generated Maven style project to Eclipse’s style project.
mvn eclipse:eclipse
Later, import the converted project into Eclipse IDE.
Create a resources folder (not folder)
Create a resources “/src/main/resources” folder, the Spring’s bean xml configuration file will put here later. Maven will treat all files under this “resources” folder as resources files, and copy it to output classes automatically.

3. Add Spring dependency

Add Spring dependency in Maven’s pom.xml file.
File : pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.mkyong.common</groupId>
 <artifactId>SpringExamples</artifactId>
 <packaging>jar</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>SpringExamples</name>
 <url>http://maven.apache.org</url>
 <dependencies>
 
  <!-- Spring framework -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring</artifactId>
   <version>2.5.6</version>
  </dependency>
 
 </dependencies>
</project>
Issue “mvn eclipse:eclipse” again, Maven will download the Spring dependency libraries automatically and put it into your Maven’s local repository. At the same time, Maven will add the downloaded libraries into Eclipse “.classpath” for dependency purpose.


I have try this one but the eclipse complained that ApplicationContext and ClassPathXmlApplicationContext cannot be resolve although the spring framework are in the lib path.

Solution :
Convert the project to maven project in Eclipse. (Download m2eclipse firstly: help->install->m2eclipse)


The reason might be that eclipse didn't know what maven is.


OK, LET'S MOVE ON...


Why use XML to config the project?
loose coupling;






Sunday, December 2, 2012