JAVA框架 Spring 注解注入

一、首先需要引入jar包:spring-aop-4.2.4.RELEASE.jar。(在spring解压包libs内)。

二、如果注解方式注入依赖的对象,需要引用新的约束。

内的:xsd-configuration.html。打开网页中的:the context schema 粘贴复制:

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4     xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
5         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
6         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
7 
8 </beans>

写接口和实现类:

1 package jd.com.inject;
2 
3 public interface Indemo {
4     void  save();
5 }

 实现类:需要写注解:@Component(value = "indemo") 其中value是配置文件中的id值,在调用的时候执行方法getbean(id值)调用就是这个值。

 1 package jd.com.inject;
 2 
 3 
 4 import org.springframework.stereotype.Component;
 5 
 6 @Component(value = "indemo")
 7 public class indemoIpl implements  Indemo {
 8     @Override
 9     public void save() {
10         System.out.println("调用业务层。");
11     }
12 }

配置文件(applicationContenxt.xml)

需要开启扫描组件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
 5         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 6         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
 7 
 8 
 9         <context:component-scan base-package="jd.com" />
10 
11 </beans>

 其中:

<context:component-scan base-package="jd.com" /> 其中base-package是需要扫描的包。需要注意:这里写的是jd.com而不是完整的包。这样涵盖所有包。

测试类:
 1 package jd.com.inject;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 public class TestDemo {
 8     @Test
 9     public  void   testdemo(){
10         ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
11         indemoIpl in= (indemoIpl) ac.getBean("indemo");
12         in.save();
13     }
14 
15 
16 }
这样就是实现注解方式注入。

三、上面是通用注解,但是spring为我们提供web层:Controller、业务层:Service、持久层:Repository spring在后续版本会对其进行增强注解。也为更清晰标注每层。
也就是上面可以写@Service注解。注解内的key也是value。
 1 package jd.com.inject;
 2 
 3 
 4 import org.springframework.stereotype.Component;
 5 import org.springframework.stereotype.Service;
 6 
 7 @Service(value = "indemo")
 8 public class indemoIpl implements  Indemo {
 9     @Override
10     public void save() {
11         System.out.println("调用业务层。");
12     }
13 }


四:依赖注解:
1)属性依赖注入:
使用注解:@Value 只能注入基本数据类型和字符串。不需要设置set方法,但是需要字段。
 1 package jd.com.inject;
 2 
 3 
 4 import org.springframework.beans.factory.annotation.Value;
 5 import org.springframework.stereotype.Component;
 6 import org.springframework.stereotype.Service;
 7 
 8 @Service(value = "indemo")
 9 public class indemoIpl implements  Indemo {
10 
11 
12     @Value(value = "属性值")
13     private  String a;
14 
15     @Override
16     public void save() {
17         System.out.println("调用业务层。"+a);
18     }
19 }
其中value是属性的值。不同于其他的注解,其他注解是getbean调用的name值。
2)引用注入:
三种方式:
1、@Autowired 自动载入,自动匹配类型和名称无关。但是一个接口如果有多个接口的话,载入类不一定是我们想要的。(不推荐使用
 1 package jd.com.inject;
 2 
 3 
 4 import jd.com.Dao.DaoIn;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.beans.factory.annotation.Value;
 7 import org.springframework.stereotype.Component;
 8 import org.springframework.stereotype.Service;
 9 
10 import javax.annotation.Resource;
11 
12 @Service(value = "indemo")
13 public class indemoIpl implements  Indemo {
14 
15     //自动载入
16     @Autowired
17     private DaoIn  daoIn;
18 
19     @Value(value = "属性值")
20     private  String a;
21 
22     @Override
23     public void save() {
24         System.out.println("调用业务层。"+a);
25         daoIn.save();
26     }
27 }


2、Qualifier强制按名称注入,需要和Autowire一起使用才生效。
 1 package jd.com.inject;
 2 
 3 
 4 import jd.com.Dao.DaoIn;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.beans.factory.annotation.Qualifier;
 7 import org.springframework.beans.factory.annotation.Value;
 8 import org.springframework.stereotype.Component;
 9 import org.springframework.stereotype.Service;
10 
11 import javax.annotation.Resource;
12 
13 @Service(value = "indemo")
14 public class indemoIpl implements  Indemo {
15 
16     //强制按名字注入
17     @Autowired
18     @Qualifier(value = "dao")
19     private DaoIn  daoIn;
20 
21     @Value(value = "属性值")
22     private  String a;
23 
24     @Override
25     public void save() {
26         System.out.println("调用业务层。"+a);
27         daoIn.save();
28     }
29 }
3、@Resource(name = "dao"):其中需要注意这里的key是name不是value。而且不属于spring框架提供的是由:javax.annotation.Resource 提供。name的值是被依赖类注入的时候的value属性值。
  还需要设置属性字段。(推荐使用
1     @Resource(name = "dao")
2     private DaoIn  daoIn;
完整代码:
 1 package jd.com.inject;
 2 
 3 
 4 import jd.com.Dao.DaoIn;
 5 import org.springframework.beans.factory.annotation.Value;
 6 import org.springframework.stereotype.Component;
 7 import org.springframework.stereotype.Service;
 8 
 9 import javax.annotation.Resource;
10 
11 @Service(value = "indemo")
12 public class indemoIpl implements  Indemo {
13 
14 
15     @Resource(name = "dao")
16     private DaoIn  daoIn;
17 
18     @Value(value = "属性值")
19     private  String a;
20 
21     @Override
22     public void save() {
23         System.out.println("调用业务层。"+a);
24         daoIn.save();
25     }
26 }
Dao层:
 1 package jd.com.Dao;
 2 
 3 
 4 import org.springframework.stereotype.Repository;
 5 
 6 @Repository(value = "dao")
 7 public class DaoImpl implements  DaoIn {
 8     @Override
 9     public void save() {
10         System.out.println("数据层数据已保存");
11     }
12 }






原文地址:https://www.cnblogs.com/evilliu/p/8868547.html