spring+mybatis通过ApplicationContext实现Autowired

原博客链接:http://blog.csdn.net/qq5132834/article/details/47870351

1、定义一个类,如下:

  1. package com.hl.demo;  
  2.   
  3. import org.springframework.stereotype.Service;  
  4.   
  5. public class Chinese {  
  6.   
  7.        
  8.     public void eat() {  
  9.         System.out.println(“eating”);  
  10.     }  
  11.   
  12.    
  13.     public void walk() {  
  14.         System.out.println(“walking”);  
  15.     }  
  16.   
  17. }  
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文件,如下:

  1. <?xml version=“1.0” encoding=“UTF-8”?>  
  2. <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=”  
  3. http://www.springframework.org/schema/beans  
  4. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  5. http://www.springframework.org/schema/context  
  6. http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  7. >  
  8.     <bean id=“CHINESE” class=“com.hl.demo.Chinese”></bean>  
  9. </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测试,如下:

  1. package com.hl.junit;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6. import org.springframework.stereotype.Component;  
  7.   
  8. import com.hl.demo.Chinese;  
  9.   
  10.   
  11.   
  12. public class text {  
  13.   
  14.       
  15.     public static void main(String[] args) {  
  16.   
  17.         ApplicationContext ctx = new ClassPathXmlApplicationContext(“/bean.xml”);  
  18.         Chinese chinese = (Chinese) ctx.getBean(“CHINESE”);    
  19.         chinese.eat();  
  20.         chinese.walk();  
  21.               
  22.           
  23.     }  
  24.       
  25.   
  26. }  
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、运行得到:

  1. eating  
  2. walking  
eating
walking


5、接下来,怎么实现@Autowired进行自动装配呢?在1中新建的Chinese类中@Service(“Chinese”),如下所示:

  1. package com.hl.demo;  
  2.   
  3. import org.springframework.stereotype.Service;  
  4.   
  5. @Service(“Chinese”)  
  6. public class Chinese {  
  7.   
  8.        
  9.     public void eat() {  
  10.         System.out.println(“eating”);  
  11.     }  
  12.   
  13.    
  14.     public void walk() {  
  15.         System.out.println(“walking”);  
  16.     }  
  17.   
  18. }  
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测试类。

  1. <?xml version=“1.0” encoding=“UTF-8”?>  
  2. <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=”  
  3. http://www.springframework.org/schema/beans  
  4. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  5. http://www.springframework.org/schema/context  
  6. http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  7. >  
  8.     <!–<bean id=”CHINESE” class=”com.hl.demo.Chinese”></bean>–>  
  9.     <bean id=“text” class=“com.hl.junit.text”></bean>     
  10.     <context:component-scan base-package=“com.hl.demo” />  
  11. </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测试函数改为如下:

  1. package com.hl.junit;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. import com.hl.demo.Chinese;  
  8.   
  9.   
  10. public class text {  
  11.   
  12.     @Autowired  
  13.     private Chinese chinese;  
  14.       
  15.     public static void main(String[] args) {  
  16.   
  17.         ApplicationContext ctx = new ClassPathXmlApplicationContext(“/bean.xml”);  
  18.         //Chinese chinese = (Chinese) ctx.getBean(“CHINESE”);    
  19.         //chinese.eat();  
  20.         //chinese.walk();  
  21.           
  22.         text a = (text) ctx.getBean(“text”);  
  23.         a.say();  
  24.           
  25.     }  
  26.       
  27.     public void say(){  
  28.         System.out.println(“hello world”);  
  29.         chinese.eat();  
  30.         chinese.walk();  
  31.     }  
  32.   
  33. }  
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、运行结果如下:

  1. hello world  
  2. eating  
  3. 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同一个类下,新建一个类如下所示:

 

  1. package com.hl.demo;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. public class Haha {  
  8.       
  9.     @Autowired  
  10.     private Chinese chinese;  
  11.       
  12.     private static Haha haha = null;  
  13.       
  14.     static{  
  15.         ApplicationContext ctx = new ClassPathXmlApplicationContext(“/bean.xml”);  
  16.         haha = (Haha) ctx.getBean(“Haha”);  
  17.         System.out.println(“hi,haha…”);  
  18.     }  
  19.       
  20.     public Haha getInstance(){  
  21.         return this.haha;  
  22.     }  
  23.       
  24.     public void xiix(){  
  25.         this.chinese.eat();  
  26.         this.chinese.walk();  
  27.     }  
  28.       
  29. }  
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修改如下所示:

  1. <%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8”%>  
  2. <%@ page language=“java” import=“com.hl.demo.*” %>  
  3. <%  
  4.     Haha haha = new Haha().getInstance();  
  5.     haha.xiix();  
  6. %>  
  7. <html>  
  8. <head>  
  9. <title>Insert title here</title>  
  10. </head>  
  11. <body>  
  12.     hello world  
  13. </body>  
  14. </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…只运行一次。

  1. 信息: Loading XML bean definitions from class path resource [bean.xml]  
  2. hi,haha…  
  3. eating  
  4. walking  
  5. eating  
  6. walking  
  7. eating  
  8. walking  
  9. eating  
  10. 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();则会一个空对象异常。

  1. <%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8”%>  
  2. <%@ page language=“java” import=“com.hl.demo.*” %>  
  3. <%  
  4.     Haha haha = new Haha().getInstance();  
  5.     haha.xiix();  
  6.       
  7.     new Haha().xiix();  
  8.       
  9. %>  
  10. <html>  
  11. <head>  
  12. <title>Insert title here</title>  
  13. </head>  
  14. <body>  
  15.     hello world  
  16. </body>  
  17. </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构造出来。类似如:
  1. ApplicationContext ctx = new ClassPathXmlApplicationContext(“/bean.xml”);  
  2.         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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值