Spring-(4)Spring 注解
来源:互联网 发布:阿里云 discuz 编辑:程序博客网 时间:2024/05/17 02:39
Spring-(4)Spring 注解
Spring基于注解的配置:
从 Spring 2.5 开始就可以使用注解来配置依赖注入。而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身。
如果需要用到注解进行依赖注入,则需要在xml文档中的beans中加上这一代码:
<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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 启用注解进行依赖注入 --> <context:annotation-config/></beans>
配置完后,你就可以开始注解你的代码,表明 Spring 应该自动连接值到属性,方法和构造函数。
下面是几个重要的注解:
各来一个例子↓直接贴代码
@Required
xml:
<!-- 使用注解方式进行方法上的自动装配 @Required --> <bean id="companyRequired" class="com.tbspringAnnotation.required.Company"> <!-- <constructor-arg index="1" value="某公司"></constructor-arg> --><!-- 构造函数赋值 --> <property name="companyName" value="某公司"></property><!-- setter赋值 --> <property name="person" ref="personRequired"></property> </bean> <bean id="personRequired" class="com.tbspringAnnotation.required.Person"> <property name="say" value="我是人,我可以说话 !"></property> </bean>
Company:
public class Company { private Person person; private String companyName; public String getCompanyName() { return companyName; } @Required //方法上的自动装配 @Required只能是setter上的注解并且这个参数的bean必须存在,否则报错 public void setCompanyName(String companyName) { this.companyName = companyName; } public Person getPerson() { return person; } @Required public void setPerson(Person person) { System.out.println("公司有很多人!"); this.person = person; }}
Person:
public class Person { private String say; public void setSay(String say) { System.out.println(say); this.say = say; }}
Main测试:
public class MainAPP { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); Company company =(Company)context.getBean("companyRequired"); }}ss
@Autowired
xml:
<!-- 使用注解方式进行方法上的/属性上的/构造函数上的自动装配 @Autowired --> <bean id="companyAutowired" class="com.tbspringAnnotation.autowired.Company"> <!-- <constructor-arg index="1" value="某公司"></constructor-arg>--><!--构造函数赋值 --> <property name="companyName" value="某公司"></property><!-- setter赋值 --> </bean> <bean id="personAutowired" class="com.tbspringAnnotation.autowired.Person"> <property name="say" value="我是人,我可以说话 !"></property> </bean>
Company:
public class Company { //@Autowired //属性上的自动装配 private Person person; private String companyName; //@Autowired //构造函数上的自动装配 /*public Company(Person person, String companyName) { System.out.println("Company :" + companyName); this.person = person; this.companyName = companyName; }*/ public String getCompanyName() { return companyName; } @Autowired(required=false) //required=false表示这个自动装配不是必须的 public void setCompanyName(String companyName) { this.companyName = companyName; } public Person getPerson() { return person; } @Autowired //方法上的自动装配 public void setPerson(Person person) { System.out.println("公司有很多人!"); this.person = person; }}
Person:
public class Person { private String say; public void setSay(String say) { System.out.println(say); this.say = say; }}
Main测试:
public class MainAPP { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); Company company =(Company)context.getBean("companyAutowired"); }}
@Qualifier
xml:
<!-- 使用注解方式进行方法上的/属性上的/构造函数上的自动装配 @Qualifier --> <bean id="companyQualifier" class="com.tbspringAnnotation.qualifier.Company"> <property name="companyName" value="某公司"></property> </bean> <bean id="personQualifier1" class="com.tbspringAnnotation.qualifier.Person" lazy-init="true"> <property name="say" value="我是人,我可以说话 !"></property> </bean> <bean id="personQualifier2" class="com.tbspringAnnotation.qualifier.Person" lazy-init="true"> <property name="say" value="哈哈哈哈哈哈哈哈哈 !"></property> </bean>
Company:
public class Company { private Person person; private String companyName; public String getCompanyName() { return companyName; } public void setCompanyName(String companyName) { this.companyName = companyName; } public Person getPerson() { return person; } @Autowired @Qualifier("personQualifier2")//指定bean中对应的名称 public void setPerson(Person person) { System.out.println("公司有很多人!"); this.person = person; }}
Person:
public class Person { private String say; public void setSay(String say) { System.out.println(say); this.say = say; }}
Main测试:
public class MainAPP { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); Company company =(Company)context.getBean("companyQualifier"); }}
最后,控制台输出:
阅读全文
0 0
- Spring-(4)Spring 注解
- 4 spring注解
- spring-4-注解实体
- spring 注解(入门)
- spring 注解(批注)
- spring注解(二)
- spring注解(二)
- spring AOP(注解)
- spring(注解方式)
- 自定义注解(spring)
- Spring注解
- Spring注解
- Spring注解
- Spring注解
- Spring注解
- Spring 注解
- Spring注解
- Spring注解
- 「NOIP2017模拟赛08.02」A**B
- 有关input输入框内容改变后的触发事件
- Bootstrap中关于input里file的样式更改
- 二维码生成与解析代码实现
- 猴子也会懂的C语言五子棋的实现方法详解
- Spring-(4)Spring 注解
- P1226 取余运算||快速幂
- HDOJ 2120 Ice_cream's world I
- stm32的LCD屏幕显示+各个模块功能调试
- Mysql基本语法汇总
- Go语言程序的命令行参数
- bzoj 4849: [Neerc2016]Mole Tunnels 模拟费用流
- 学习akka之Router
- 事物—事物的隔离级别