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");
}
}
2、配置bean.xml文件,如下:
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();
}
}
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");
}
}
6、将2中的配置文件bean.xml改为如下,其中com.hl.junit.text为3中的main测试类。
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();
}
}
可以看到,Chinese没有进行初始化,而直接用@Autowired完成自动注入的。
8、运行结果如下:
hello world
eating
walking
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();
}
}
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();
%>
Insert title here
hello world
信息: 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();
%>
Insert title here
hello world
13、由此也印证上面第9点提到的,如果要使用spring自动注入,那么使用自动注入的母类也必须是通过spring构造出来。类似如:
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