博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring 注解总结
阅读量:4209 次
发布时间:2019-05-26

本文共 6835 字,大约阅读时间需要 22 分钟。

@Component注解

默认注入的bean的id属性值为类名首字符小写

也可以在注入的时候指定id值

@Component()public class BeanAnnotation {    public void say(){        System.out.println("SAY SOMETHING!");    }}
BeanAnnotation annotation = (BeanAnnotation) applicationContext1.getBean("beanAnnotation");        annotation.say();

@Required注解

适用于bean属性的setter方法

表示该属性配置时必须被填充

public class BeanAnnotation {    private String name;    @Required    public void setName(String name) {        this.name = name;    }    public void say(){        System.out.println("SAY SOMETHING!");    }}
BeanAnnotation annotation = (BeanAnnotation) applicationContext1.getBean("beanAnnotation");        annotation.say();

否则会报错

Caused by: org.springframework.beans.factory.BeanInitializationException: Property 'name' is required for bean 'beanAnnotation'CapableBeanFactory.java:553)    ... 11 more

@Autowired注解

自动装配

可以用于bean的setter方法,构造器,构造函数

1.用在Field上

@Repositorypublic class AutoWiredDao {    public void say(){        System.out.println("Autowired....");    }}
@Servicepublic class AutoWiredServiceImpl implements AutoWiredService {
@Autowired//加在bean上 private AutoWiredDao autoWiredDao; @Override public void say() { autoWiredDao.say(); }}

2.加在setter方法上

@Autowired//加在setter方法上    public void setAutoWiredDao(AutoWiredDao autoWiredDao) {        this.autoWiredDao = autoWiredDao;    }

3.加在构造函数上

@Autowired//加在constructor上    public AutoWiredServiceImpl(AutoWiredDao autoWiredDao) {        this.autoWiredDao = autoWiredDao;    }

4.加在List上 把对象放入List

5.加在Map上 把Id和对象放入Map

使用@Oreder进行排序 只对List有效

/** * BeanInterface * 测试@Autowired注解List,Map,Set * Created by heqianqian on 2017/4/25. */public interface BeanInterface {
}
/** * BeanImplOne * 测试@Autowired注解List,Map,Set * Created by heqianqian on 2017/4/25. */@Order(2)@Componentpublic class BeanImplOne implements BeanInterface {
}
/** * BeanImplTwo * 测试@Autowired注解List,Map,Set * Created by heqianqian on 2017/4/25. */@Order(1)@Componentpublic class BeanImplTwo implements BeanInterface {
}
@Componentpublic class BeanInvoker {    @Autowired    private List
beanInterfaceList; @Autowired private Map
beanInterfaceMap; public void autowireList() { System.out.println("===autowireList==="); if (null != beanInterfaceList) { for (BeanInterface beanInterface : beanInterfaceList) { System.out.println(beanInterface.getClass().getName()); } } else { System.out.println("beanInterfaceList is null"); } } public void autowireMap() { System.out.println("===autowireMap==="); if (null != beanInterfaceMap) { for (Map.Entry
beanInterfaceEntry : beanInterfaceMap.entrySet()) { System.out.println(beanInterfaceEntry.getKey() + " " + beanInterfaceEntry.getValue().getClass().getName()); } } else { System.out.println("beanInterfaceMap is null"); } }}
BeanInvoker beanInvoker = (BeanInvoker) applicationContext1.getBean("beanInvoker");        beanInvoker.autowireList();        beanInvoker.autowireMap();

输出结果

===autowireList===  bean.annotation.BeanImplTwobean.annotation.BeanImplOne===autowireMap===beanImplOne bean.annotation.BeanImplOnebeanImplTwo bean.annotation.BeanImplTwo

@Qualifier注解

按类型装配多个可能bean实例的情况,可以缩小范围或者指定唯一bean,也可以用于指定构造器参数或者方法参数

可以注解集合类型变量

@Autowired    @Qualifier("beanImplOne")    private BeanInterface beanInterface;
public void qualifier(){        System.out.println("===qualifier===");        if (beanInterface!=null){            System.out.println(beanInterface.getClass().getName());        }    }

@Bean注解

@Bean+@Configuration

用于配置和初始化一个由SpringIoC容器管理的新对象的方法,类似于XML配置文件的<bean/>

@Configurationpublic class AppConfig {
@Bean public AutoWiredService myService(){ return new AutoWiredServiceImpl(); }}

等同于在XML文件中配置

bean没有指定name时默认id是方法的名称

@Bean+@Scope

@Bean默认的单例模式Singleton

使用@Scope的value属性指定作用域(singleton,prototype,request,session,global session)

proxyMode指定代理方式

@ImportResource,@PropertyResource和@Value注解

一般引入配置文件的方式

url=jdbc:mysql://localhost:3306/pro_managedriver=com.mysql.jdbc.Driverusername=rootpassword=root

使用注解引入配置文件的方法

public class MyDriverManager {    public MyDriverManager(String url, String driver, String username, String pwd) {        System.out.println("url:" + url);        System.out.println("driver:" + driver);        System.out.println("username:" + username);        System.out.println("pwd:" + pwd);    }}
@Configuration@PropertySource("classpath:jdbc.properties")public class AppConfig {
@Value("${url}") private String url; @Value("${driver}") private String driver; @Value("${username}") private String username; @Value("${password}") private String password; @Bean public MyDriverManager myDriverManager() { return new MyDriverManager(url, driver, username, password); }}
MyDriverManager myDriverManager = (MyDriverManager) applicationContext1.getBean("myDriverManager");        System.out.println(myDriverManager.getClass().getName());

输入结果

Loading XML bean definitions from class path resource [application-anno.xml]url:jdbc:mysql://localhost:3306/pro_managedriver:com.mysql.jdbc.Driverusername:heqianqianpwd:root===init StringStore===Disconnected from the target VM, address: '127.0.0.1:53370', transport: 'socket'bean.annotation.MyDriverManager

这里出现一个小问题

输入的username和配置文件的username不同 这里取到的是当前操作系统的用户名 所以建议修改配置文件 改成 数据库驱动.username的形式

jdbc.url=jdbc:mysql://localhost:3306/pro_managejdbc.driver=com.mysql.jdbc.Driverjdbc.username=rootjdbc.password=root

这样打印出来的结果就是正确的了

Loading XML bean definitions from class path resource [application-anno.xml]url:jdbc:mysql://localhost:3306/pro_managedriver:com.mysql.jdbc.Driverusername:rootpwd:root===init StringStore===bean.annotation.MyDriverManager

@Resource和@PostConstruct,@PreDestroy

没有显示指定name默认从属性名和setter方法得出

@Repositorypublic class ResouceDao {    public void save() {        System.out.println("ResouceDao Save");    }    @PostConstruct    public void init() {        System.out.println("ResouceDao Init");    }    @PreDestroy    public void destroy() {        System.out.println("ResouceDao Destroy");    }}
@Servicepublic class ResourceService {
@Resource private ResouceDao resouceDao; //@Resource //public void setResouceDao(ResouceDao resouceDao) {
// this.resouceDao = resouceDao; //} public void save() { resouceDao.save(); }}
ResourceService resourceService = (ResourceService) applicationContext1.getBean("resourceService");        resourceService.save();

JSR330

首先引入jar包

javax.inject
javax.inject
1

@Inject

等同于@AutoWired

可以用于构造器,Setters和类

@Named

与@Component等效


转载地址:http://ixqli.baihongyu.com/

你可能感兴趣的文章
0215 docker环境
查看>>
0216 aop和打印数据库执行日志
查看>>
0219 springmvc-拦截器和响应增强
查看>>
0223 研发工程师如何提高接口质量?
查看>>
0224 如何面对高并发?缓存?中台为什么会火?
查看>>
0226 rest接口设计
查看>>
0228 我的潘多拉
查看>>
0302 中台落地前概念和思考
查看>>
0228 我的潘多拉
查看>>
0302 中台落地前概念和思考
查看>>
0219 springmvc-拦截器和响应增强
查看>>
0308 软件系统的非功能需求
查看>>
0308 软件系统的非功能需求
查看>>
0309 软件基本原理1
查看>>
0312 java接口测试三棱军刺rest-assured
查看>>
0318 guava并发工具
查看>>
0312 java接口测试三棱军刺rest-assured
查看>>
中台之中台的设计
查看>>
mysql之事务
查看>>
中台之交付
查看>>