1.通过注解创建对象
可以用于创建对象的注解
以下注解可以创建对象:
@Component:可以创建任意的对象,什么对象都可以用它来创建
@Controlller:专门用来创建界面层,Controller层的对象
@Service:专门用来创建业务逻辑层,Service层的对象
@Repository:专门用来创建数据访问层,dao层/mapper层的对象
你想创建哪个类的对象,就在这个类前面加上Componet注解
//表示告诉spring要创建一个Student类的对象,名字叫myStudent,创建出来的对象放在容器中
//也就是相当于Student myStudent=new Student();
@Component("myStudent")
public class Student
{
private String name;
private Integer age;
}
如果直接写@Componet,没有后面的("myStudent”,那么默认你创建的对象名字就是这个类的首字母小写)
这个类叫Student,所以给你创建的类的名字就叫student
测试代码:
String config= "applicationContext.xml";
ApplicationContext ctx=new ClassPathXmlApplicationContext(config);
//上面创建好了一个容器对象
//从容器中找到名字叫做myStudent的对象
Student student= (Student) ctx.getBean("myStudent");
System.out.println("student=="+student);
2.通过注解给对象的普通属性赋值
(1)在想要赋值的属性上面输入@Value注解即可(更推荐这种方式,不推荐下面第二种方式)
@Component("myStudent")
public class Student
{
@Value("李四")
private String name;
@Value("20")
private Integer age;
}
(2)也可以在对应的set方法上使用@Value注解
@Component("myStudent")
public class Student
{
private String name;
private Integer age;
public String getName() {
return name;
}
@Value("李四")
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
@Value("20")
public void setAge(Integer age) {
this.age = age;
}
}
(3)@Value注解+外部属性配置文件(properties文件)
在resources这个文件夹下main放一个myconf.properties文件,文件内容:
myname=张三
myage=28
然后在配置文件中:增加一个property-placeholder标签
<context:property-placeholder location="classpath:/myconf.properties"/>
最后在类中的属性上面或者set方法上面:
@Component(value="myStudent")
public class Student
{
@Value("${myname}")
private String name;
@Value("${myage}")
private Integer age;
}
3.通过注解给 对象属性赋值
对象属性就是 属性是一个对象
有两种方式给对象属性赋值:
(1)单独使用@Autuwired 按类型注入 byType
从整个bean工厂中搜索和 的类相同的类的对象,进行赋值
比如说:private School school,那么对象属性的类就是School类,需要去bean工厂里面找School类的对象,注入给这个对象属性
step1:这是一个School类
通过@Component注解创建一个School类的对象,对象名叫mySchool
@Component("mySchool")
public class School
{
@Value("北京大学")
private String name;
@Value("北京海淀区")
private String address;
}
step2:这是一个Student类
先创建了一个Student类的对象,名字叫作myStudent
然后重点是我们要给这个对象的对象属性进行赋值
@Component("myStudent")
public class Student
{
@Value("张三")
private String name;
@Value("20")
private Integer age;
@Autowired
//默认使用byType
private School school;
}
spring会搜索容器内有没有对象的类型是School的,显然上面的mySchool这个对象就是School类型的对象,于是就会将mySchool对象作为属性赋值给myStudent这个对象的school属性
(2)@Autowired+@Qualified 按名字注入 byName注入
@Component("mySchool")
public class School
{
@Value("北京大学")
private String name;
@Value("北京海淀区")
private String address;
}
@Component("myStudent")
public class Student
{
@Value("张三")
private String name;
@Value("20")
private Integer age;
@Autowired
@Qualifier("mySchool")
private School school;
}
在容器内找,将名字叫作mySchool的对象赋值给myStudent对象的school属性
给对象属性赋值之后可以测试一下:
String config= "applicationContext.xml";
ApplicationContext ctx=new ClassPathXmlApplicationContext(config);
//上面创建好了一个容器对象
//下面创建一个Student类对象
Student student= (Student) ctx.getBean("myStudent");
System.out.println(student.getAge());
System.out.println(student.getName());
System.out.println(student.getSchool().getAddress());
System.out.println(student.getSchool().getName());
注意:还有一个注解@Resource也可以完成给对象的属性进行赋值
将@Autowired改为@Resource即可
@Component("myStudent")
public class Student
{
@Value("张三")
private String name;
@Value("20")
private Integer age;
@Resource
private School school;
}
比较有意思的是,@Resource注解默认使用byName自动注入,如果找不到需要的对象,就使用byType自动注入
@Resource注解只使用byName自动注入:@Resource(name="mySchool")