spring+mybatis通过ApplicationContext实现Autowired
来源:互联网 发布:旅行商问题算法 编辑:程序博客网 时间:2024/06/12 18:13
原博客链接: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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://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
eatingwalking
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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://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 worldeatingwalking
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...eatingwalkingeatingwalkingeatingwalkingeatingwalking
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
阅读全文
0 0
- spring+mybatis通过ApplicationContext实现Autowired
- spring ApplicationContext.xml配置mybatis
- Spring,通过ApplicationContextAware获取ApplicationContext
- mybatis+spring+struts2主配置文件applicationContext.xml
- spring整合mybatis的applicationContext.xml配置文件
- spring、mybatis整合核心配置文件applicationContext.xml
- MyBatis-Spring配置简单了解-applicationContext.xml
- spring结合mybatis 用@autowired 方式调用mybatis接口
- Spring通过ApplicationContext主动获取bean
- 通过ApplicationContextAware接口来保存Spring ApplicationContext
- Spring通过ApplicationContext主动获取bean
- 2.4 通过ApplicationContext接口获取Spring容器
- Spring MVC @Autowired、@Controller、@Service 的实现
- [spring]@Autowired
- Spring @Autowired
- @Autowired spring
- Spring Autowired
- spring @Autowired
- Hadoop之HBase框架学习基础实践篇(笔记16)
- 【NIO总结】—NIO中的缓冲区
- SpringBoot注解说明
- 机器学习之决策树预测——泰坦尼克号乘客数据实例
- IP制作流竟然离我如此之近
- spring+mybatis通过ApplicationContext实现Autowired
- 《剑指offer》求1+2+3+...n(不用if、else、乘除等)
- spark--transform算子--sortByKey
- 安卓 MediaCodec解码aac
- 设置UITextView的行间距
- 自定义视图布局的时候,获取相关坐标
- Hadoop之Pig框架学习(笔记17)
- 高级几何的介绍
- 【文献阅读】Augmenting Supervised Neural Networks with Unsupervised Objectives-ICML-2016