原博客链接:http://blog.csdn.net/qq5132834/article/details/47870351
1、定义一个类,如下:
- package com.hl.demo;
- import org.springframework.stereotype.Service;
- public class Chinese {
- public void eat() {
- System.out.println(“eating”);
- }
- public void walk() {
- System.out.println(“walking”);
- }
- }
package com.hl.demo;
import org.springframework.stereotype.Service;
public class Chinese {
public void eat() {
System.out.println("eating");
}
public void walk() {
System.out.println("walking");
}
}
2、配置bean.xml文件,如下:
- <?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
- “>
- <bean id=“CHINESE” class=“com.hl.demo.Chinese”></bean>
- </beans>
<?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
">
<bean id="CHINESE" class="com.hl.demo.Chinese"></bean>
</beans>
3、新建一个main测试,如下:
- package com.hl.junit;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.stereotype.Component;
- import com.hl.demo.Chinese;
- public class text {
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext(“/bean.xml”);
- Chinese chinese = (Chinese) ctx.getBean(“CHINESE”);
- chinese.eat();
- chinese.walk();
- }
- }
package com.hl.junit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import com.hl.demo.Chinese;
public class text {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/bean.xml");
Chinese chinese = (Chinese) ctx.getBean("CHINESE");
chinese.eat();
chinese.walk();
}
}
4、运行得到:
- eating
- walking
eating
walking
5、接下来,怎么实现@Autowired进行自动装配呢?在1中新建的Chinese类中@Service(“Chinese”),如下所示:
- package com.hl.demo;
- import org.springframework.stereotype.Service;
- @Service(“Chinese”)
- public class Chinese {
- public void eat() {
- System.out.println(“eating”);
- }
- public void walk() {
- System.out.println(“walking”);
- }
- }
package com.hl.demo;
import org.springframework.stereotype.Service;
@Service("Chinese")
public class Chinese {
public void eat() {
System.out.println("eating");
}
public void walk() {
System.out.println("walking");
}
}
6、将2中的配置文件bean.xml改为如下,其中com.hl.junit.text为3中的main测试类。
- <?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
- “>
- <!–<bean id=”CHINESE” class=”com.hl.demo.Chinese”></bean>–>
- <bean id=“text” class=“com.hl.junit.text”></bean>
- <context:component-scan base-package=“com.hl.demo” />
- </beans>
<?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
">
<!--<bean id="CHINESE" class="com.hl.demo.Chinese"></bean>-->
<bean id="text" class="com.hl.junit.text"></bean>
<context:component-scan base-package="com.hl.demo" />
</beans>
7、将上3中的main测试函数改为如下:
- package com.hl.junit;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.hl.demo.Chinese;
- public class text {
- @Autowired
- private Chinese chinese;
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext(“/bean.xml”);
- //Chinese chinese = (Chinese) ctx.getBean(“CHINESE”);
- //chinese.eat();
- //chinese.walk();
- text a = (text) ctx.getBean(“text”);
- a.say();
- }
- public void say(){
- System.out.println(“hello world”);
- chinese.eat();
- chinese.walk();
- }
- }
package com.hl.junit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hl.demo.Chinese;
public class text {
@Autowired
private Chinese chinese;
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/bean.xml");
//Chinese chinese = (Chinese) ctx.getBean("CHINESE");
//chinese.eat();
//chinese.walk();
text a = (text) ctx.getBean("text");
a.say();
}
public void say(){
System.out.println("hello world");
chinese.eat();
chinese.walk();
}
}
可以看到,Chinese没有进行初始化,而直接用@Autowired完成自动注入的。
8、运行结果如下:
- hello world
- eating
- walking
hello world
eating
walking
9、为什么要讲text测试main类加入到spring中呢。这是因为,如果要让mian这个text类中的private Chinese chinese;由spring自动注入,那么则必须要求text则类本身也是由spring构造出来的。换言之,就是要讲text这个类也构成一个bean才可以。如果是new出来的text对象,则肯定不会讲chinese自动注入,会报空对象异常。
10、将Autowired最简单的自动注入到Java web中去,在刚才的pageage com.hl.demo中的Chinese同一个类下,新建一个类如下所示:
- package com.hl.demo;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Haha {
- @Autowired
- private Chinese chinese;
- private static Haha haha = null;
- static{
- ApplicationContext ctx = new ClassPathXmlApplicationContext(“/bean.xml”);
- haha = (Haha) ctx.getBean(“Haha”);
- System.out.println(“hi,haha…”);
- }
- public Haha getInstance(){
- return this.haha;
- }
- public void xiix(){
- this.chinese.eat();
- this.chinese.walk();
- }
- }
package com.hl.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Haha {
@Autowired
private Chinese chinese;
private static Haha haha = null;
static{
ApplicationContext ctx = new ClassPathXmlApplicationContext("/bean.xml");
haha = (Haha) ctx.getBean("Haha");
System.out.println("hi,haha...");
}
public Haha getInstance(){
return this.haha;
}
public void xiix(){
this.chinese.eat();
this.chinese.walk();
}
}
11、在index.jsp修改如下所示:
- <%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8”%>
- <%@ page language=“java” import=“com.hl.demo.*” %>
- <%
- Haha haha = new Haha().getInstance();
- haha.xiix();
- %>
- <html>
- <head>
- <title>Insert title here</title>
- </head>
- <body>
- hello world
- </body>
- </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page language="java" import="com.hl.demo.*" %>
<%
Haha haha = new Haha().getInstance();
haha.xiix();
%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
hello world
</body>
</html>
打印的结果如下:注意hi,haha…只运行一次。
- 信息: Loading XML bean definitions from class path resource [bean.xml]
- hi,haha…
- eating
- walking
- eating
- walking
- eating
- walking
- eating
- walking
信息: Loading XML bean definitions from class path resource [bean.xml]
hi,haha...
eating
walking
eating
walking
eating
walking
eating
walking
12、如果将index.jsp中的代码改为如下,即添加一个new Haha().xiix();则会一个空对象异常。
- <%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8”%>
- <%@ page language=“java” import=“com.hl.demo.*” %>
- <%
- Haha haha = new Haha().getInstance();
- haha.xiix();
- new Haha().xiix();
- %>
- <html>
- <head>
- <title>Insert title here</title>
- </head>
- <body>
- hello world
- </body>
- </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page language="java" import="com.hl.demo.*" %>
<%
Haha haha = new Haha().getInstance();
haha.xiix();
new Haha().xiix();
%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
hello world
</body>
</html>
13、由此也印证上面第9点提到的,如果要使用spring自动注入,那么使用自动注入的母类也必须是通过spring构造出来。类似如:
- ApplicationContext ctx = new ClassPathXmlApplicationContext(“/bean.xml”);
- haha = (Haha) ctx.getBean(“Haha”);
ApplicationContext ctx = new ClassPathXmlApplicationContext("/bean.xml");
haha = (Haha) ctx.getBean("Haha");
所以在java web中,采用spring的自动注入,一般和spring mvc或者strus混搭使用。下面,我们单纯讲及spring mvc的controller和自动注入的搭配使用。这些会在下一篇spring mvc的学习博客讲到,而到目前为止,我们都还没有对web.xml文件作任何操作。
14、源代码:http://download.csdn.NET/detail/qq5132834/9171379