首页 > 编程语言 > 详细

3、spring注解注入

时间:2014-04-07 10:02:51      阅读:623      评论:0      收藏:0      [点我收藏+]

1.写需要注解注入的类:

Propertie.java

package study;

public class Propertie {

    public void show() {

        System.out.print("我是注解注入的!");

    }

}

2.Person接口:

package study;
public interface Person {
     public void output();
 }
3.Chinese实现Person接口:
package study;
//必要的包
import javax.annotation.Resource;

public class Chinese implements Person {
        //注解注入符合@Resource
    @Resource private Propertie p;
    private String name;
        public void output() {
        p.show();
    }
}
4.测试类TestPerson.java
package study;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestPerson {

    /**
     * @param args
     */

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean3.xml");
        Person p = (Person) ctx.getBean("chinese");
        p.output();
    }

}
5.bean3.xml配置文件
<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"
>
         <context:annotation-config/>
         <bean id="p" class="study.Propertie">
         </bean>
         <bean id="chinese" class="study.Chin
         ese"
>
              <property name="name">
                  <value>张敏鹏</value>
              </property>
         </bean> 
</beans>
   <!-- 
                      注解所引用的:
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation=" http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
    -->

6.运行结果:

bubuko.com,布布扣





3、spring注解注入,布布扣,bubuko.com

3、spring注解注入

原文:http://www.cnblogs.com/zmpandzmp/p/3648811.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!