@Required注释

作用:用于属性的set方法,那么这个属性必须在xml文件的bean标签里面进行配置,否则就会抛出一个BeanInitializationException异常。

首先准备一个类:

public class Person{
private int age;
private String name;
@Required
public void setAge(int age) {
this.age = age;
}
public Integer getAge() {
return age;
}
@Required
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

再准备一个测试类:

public class MainApp{
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("person.xml");
Person person = (Person) context.getBean("p");
System.out.println("Name : " + student.getName() );
System.out.println("Age : " + student.getAge() );
}
}

配置文件内容如下:

<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
   //必须加上这个标签,后面几个注释的配置文件也一样。 
<context:annotation-config/> <bean id="p" class="Annotations.injection.Person">
<property name="name" value="张三 />
<property name="age" value="18"/>
</bean>
</beans>

大家可以自行把<property name="age" value="11"/>或者 <property name="name" value="Zara" />注释掉,看看是否会报错。上面的代码才是完整的。

输出结果:

Name : 张三
Age : 11

@Autowired

作用:这个标签在不同的部位前面,作用也不一样。但是总的来说,作用基本和其名字一样,自动连线,只是连线对象不一样了。

当在一个set方法前,即使我们没在bean里面配置他的值或引用,它也会在beans里面寻找相同类型的bean去匹配,就如byType一样。

当在一个属性前,这个属性可以不需要set方法。

当在一个构造函数前,尤其是有参构造函数,即使我们不给这个构造函数传参,它也会在beans里寻找相同类型的bean,并传递给这个构造函数。

下面分别演示和对比其作用。

在set方法前

首先准备一个类,在其set方法前加上Autowired注释:

public class Hello {
private Hello_Son hs;
public Hello_Son getHs() {
return hs;
}
@Autowired
public void setHs(Hello_Son hs) {
this.hs = hs;
}
}

为了方便演示,再准备一个类当自定义类型:

public class Hello_Son {
public Hello_Son(){
System.out.println("这是hello_son的无参构造函数");
}
}

测试类:

public class MainApp {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("ann_bean.xml");
Hello h=(Hello)context.getBean("hello");
h.getHs();
}
}

区别对比:

使用了Autowired注释的配置文件内容如下:

<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="hello" class="Annotations.injection.Hello">
</bean>
<bean id="hs" class="Annotations.injection.Hello_Son"/>
</beans>

不使用Autowired注释的配置文件内容如下:

<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="hello" class="Annotations.injection.Hello">
<!-- 需要手动配置-->
<property name="hs" ref="hs"></property>
</bean>
<bean id="hs" class="Annotations.injection.Hello_Son"/>
</beans>

运行结果:

这是hello_son的无参构造函数

在属性前(在set方法前和在属性前结果一样,所以一般用这个)

首先准备一个类,无需set方法:

public class Hello {
@Autowired
private Hello_Son hs; public Hello_Son getHs() {
return hs;
} }

自定义类型类和上面一样。

配置文件和上面一样。

区别对比:

使用了Autowired注释的属性,不需要set方法。

不使用Autowired注释的属性,需要set方法。

运行结果:

这是hello_son的无参构造函数

在构造函数前

首先准备一个类:

public class Hello {
private Hello_Son hs;
@Autowired
public Hello(Hello_Son hs) {
System.out.println("这是hello的有参构造函数");
this.hs=hs;
}
public Hello_Son getHs() {
return hs;
}
}

自定义类型类和配置文件不变。

区别对比:

使用了Autowired注释的构造函数的配置文件:

<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="hello" class="Annotations.injection.Hello">
</bean>
<bean id="hs" class="Annotations.injection.Hello_Son"/>
</beans>

不使用Autowired注释的构造函数的配置文件:

<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="hello" class="Annotations.injection.Hello">
<constructor-arg ref="hs"/>
</bean>
<bean id="hs" class="Annotations.injection.Hello_Son"/>
</beans>

运行结果:

这是hello_son的无参构造函数
这是hello的有参构造函数

@Qualifier

作用:当创建多个相同类型的bean时,在使用时,只需要配置其中一个,那么这时候就可以使用@Qualifier注释。

首先创建一个类:

public class students {
private int age;
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public void setAge(int age) {
this.age = age;
} public int getAge() {
return age;
}
}

再为学生类创建一个配置类,方便使用:

public class stu_profile {
@Autowired
@Qualifier("stu1")
private students stu; public stu_profile() {
System.out.println("配置类的构造函数");
} public void getStu() {
System.out.println("名字叫:" + stu.getName() + ";" + "年龄:" + stu.getAge());
}
}

可以看到,配置类中指定了stu1,这个stu1就是一个bean的id。并和@Autowired一起使用,这样就不用再写个set方法了。

配置文件内容如下:

<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/>
<bean id="profile" class="Annotations.injection.stu_profile"></bean>
<bean id="stu1" class="Annotations.injection.students">
<property name="age" value="18"/>
<property name="name" value="张三"/>
</bean>
<bean id="stu2" class="Annotations.injection.students">
<property name="name" value="李四"/>
<property name="age" value="19"/>
</bean> </beans>

测试类:

public class MainApp {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("students.xml");
stu_profile stu=(stu_profile)context.getBean("profile");
stu.getStu();
}
}

运行结果:

配置类的构造函数
名字叫:张三;年龄:18

最新文章

  1. Django--models多对多
  2. 用Sublime Text搭建简易IDE编写Verilog代码
  3. 《深入浅出 Java Concurrency》
  4. scala言语基础学习十二
  5. mongoDB 下载/安装/客户端笔记
  6. Reduce对Pig作业性能的影响
  7. 【HTML】Advanced5:Accessible Forms
  8. 安装sql server 2008,提示要删除SQL Server 2005 Express 工具 怎么解决?
  9. python 网络编程第三版
  10. 京东金融集团BD部门招聘 BD经理
  11. jQuery 属性操作 - attr() 方法
  12. MVC view视图获取Html.RenderAction方式带来的参数
  13. Java程序员入门:程序员究竟可以干多少年?
  14. HDU1248--完全背包
  15. iOS 正则表达式使用(转)
  16. java线程与进程
  17. TPshop之短信注册配置(阿里云)
  18. Oracle 《积累章》 根据身份证号码更新当前出生日期
  19. Coding and Paper Letter(五十八)
  20. IdentityServer4授权类型(GrantType)对应的返回类型(ResponseType)

热门文章

  1. Opentracing + Uber Jaeger 全链路灰度调用链,Nepxion Discovery
  2. markdown语法(转)
  3. LeetCode刷题笔记(3)Java位运算符与使用按位异或(进制之间的转换)
  4. Unity 单例模式
  5. 当node版本升级到8.0以上带来的问题
  6. 【MySQL】MySQL服务启动失解决方法
  7. 【Java】Windows配置Java环境变量
  8. 对pwntools生成的exp模版做了一些修改
  9. thinkphp5.1长连接-单例模式测试!
  10. VMware下Centos7-Minimal上网配置