On the way learning spring
Spring 5 - "HelloWorld"
1.Bulid the maven project-->check the Create a simple project
Set Group Id: com.caveofprogramming.spring.test
Artifact Id: spring-tutorial5-->finish project Buliding
2.Right click project --> create a Java Class
Set Source floder: spring-tutorial5/src/main/java
Package: com.caveofprogramming.spring.test
Name: App
3.In the package you created before, create another Java Class "Person"
4.Open pom.xml file, use tags below to download .jar files you need, for example
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
</dependencies>
5. Right click project--> Create a Spring Bean Configuration file"beans.xml"
6. Create new Bean "Person" in "beans.xml"(Use interface). The source code is
<bean id="person"
class="com.caveofprogramming.spring.test.Person">
</bean>
Spring 6 - ClassPathContexts (spring-tutorial5)
package com.caveofprogramming.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args){
//Create spring beans container
ApplicationContext context = new ClassPathXmlApplicationContext("src/main/java/com/caveofprogramming/spring/test/beans/beans.xml");
ApplicationContext context = new ClassPathXmlApplicationContext("com/caveofprogramming/spring/test/beans/beans.xml");
Person person = (Person)context.getBean("person");
person.speak();
((ClassPathXmlApplicationContext)context).close();
}
}
Green Url is the right one. (This is for ClassPathXmlApplicationContext. There are difference between ClassPathXmlApplicationContext and FileSystemXmlApplicationContext)
Spring 7 -Constructor Argument (spring-tutorial5)
1. Create new package com.caveofprogramming.spring.test.beans --> drug the "beans.xml" file into it.
2.Insert<constuctor-arg> element
In the "beans.xml", right click "Person" bean-->choose Insert<constuctor-arg> element
Then you can set attribute in it. The source code in it is:
<bean id="person"
class="com.caveofprogramming.spring.test.Person">
<constructor-arg value="777" name="id"></constructor-arg>
<constructor-arg value="Mary" name="name"></constructor-arg>
</bean>