Spring4.0学习(二)采用@Autowired方式自动获取
2015-08-23 01:07
633 查看
1、定义一个类,如下:
3、新建一个main测试,如下:
4、运行得到:
5、接下来,怎么实现@Autowired进行自动装配呢?在1中新建的Chinese类中@Service("Chinese"),如下所示:
8、运行结果如下:
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同一个类下,新建一个类如下所示:
11、在index.jsp修改如下所示:
打印的结果如下:注意hi,haha...只运行一次。
12、如果将index.jsp中的代码改为如下,即添加一个new Haha().xiix();则会一个空对象异常。
14、源代码:http://download.csdn.net/detail/qq5132834/9171379
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>
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(); } }
4、运行得到:
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测试类。
<?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(); } }可以看到,Chinese没有进行初始化,而直接用@Autowired完成自动注入的。
8、运行结果如下:
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(); } }
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>
打印的结果如下:注意hi,haha...只运行一次。
信息: 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>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
相关文章推荐
- eclipse debug source not fount
- SpringMVC原理流程图
- struts 2 用s 标签 输出 list 里的 map
- springboot 开发入门,及问题汇总
- ubuntu14.04lts如何使用PPA安装JDK8
- springboot 开发入门,及问题汇总
- springboot 开发入门,及问题汇总
- springboot 开发入门,及问题汇总
- 每天五个java相关面试题(8)--spring篇
- Java -- XML解析工具dom4j
- spring配置文件详解
- Java深入学习之--初始化
- for(int a:i)在java 编程中的使用
- 从Eclipse转移到IntelliJ IDEA一点心得
- 关于javaSE面向对象的一些小知识
- struts2的优点
- 搭建struts2的开发环境
- java线程中join方法原理,以及守护线程的使用
- 解决Spring定时计划任务重复执行两次(实例被构造两次)问题的方法
- 在Controller中通过非注解的方式获取spring和springMVC上下文的方式