spring基于注解的方式创建对象1)简单类型、对象类型属性的赋值
1. 创建对象
1 package com.bjpowernode.ba01; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.beans.factory.annotation.Qualifier; 5 import org.springframework.beans.factory.annotation.Value; 6 import org.springframework.stereotype.Component; 7 8 /** 9 * @component:创建对象的,等同于<bean/>的功能 10 * 属性:value 就是对象的名称,也就是bean的id值 11 * value的值是唯一的,创建的对象在整个spring容器中就一个 12 * 位置:在类的上面 13 * @component(value="myStudent")等同于 14 * <bean id="myStudent" class="com.bjpowernode.ba01.Student"/> 15 * 16 * spring中和@component功能一致,创建对象的注解还有 17 * 1. @Respository(用在持久层类的上面):放在dao的实现类上面, 18 * 标识创建dao对象,dao对象是能访问数据库的 19 * 2. @Service(用在业务层类的上面):放在service的实现类上面 20 * 创建service对象,service对象是做业务处理,可以有事务等功能的 21 * 3. @Controller(用在控制器的上面):放在控制器(处理器)类的上面,创建控制器对象的 22 * 控制器对象,能够接受用户提交的参数,显示请求的处理结果 23 * 以上三个注解的使用语法和@component一样的,都能创建对象,但是这三个注解还有额外的功能 24 * @Respository ,@Service,@Controller是个项目的对象分层的 25 */ 26 @Component(value = "myStudent") 27 public class Student { 28 /** 29 * @Value(value="")简单类型的属性赋值 30 * 1. 可以放到属性定义的上面,无需set方法 31 * 2. 在set方法的上面 32 */ 33 @Value(value = "张三") 34 private String name; 35 @Value(value = "22") 36 private Integer age; 37 /** 38 * @AutoWired对象属性的赋值(可以放到属性定义上面,也可以放到set方法的上面) 39 * 默认是byType的方式 40 * 如果要想使用byName方式,需要做的是 41 * 1. 在属性上面加入@AutoWired 42 * 2. 在属性上面加入@Qualifier(value="bean的id") 43 */ 44 @Autowired 45 @Qualifier("school") 46 private School school; 47 public void setName(String name) { 48 this.name = name; 49 } 50 51 public void setAge(Integer age) { 52 this.age = age; 53 } 54 55 @Override 56 public String toString() { 57 return "Student{" + 58 "name='" + name + '\'' + 59 ", age=" + age + 60 ", school=" + school + 61 '}'; 62 } 63 }
/**
* @AutoWired对象属性的赋值(可以放到属性定义上面,也可以放到set方法的上面)
* 默认是byType的方式
* 如果要想使用byName方式,需要做的是
* 1. 在属性上面加入@AutoWired
* 2. 在属性上面加入@Qualifier(value="bean的id")
* 如果依赖的属性对象不存在,默认会报错(@Qualifier的require=true,如果想不抱错可以使用false)
* @Resource对象属性赋值(jdk实现的注解,可以放到属性定义上面,也可以放到set方法的上面)
* 默认是myName的方式,如果标签的id没有找到,就使用byType方式进行赋值
* 如果只想使用byName的方式,则需要加上name=""
*/
@Resource(name = "mySchool")
1 package com.bjpowernode.ba01; 2 3 import org.springframework.beans.factory.annotation.Value; 4 import org.springframework.stereotype.Component; 5 6 @Component 7 public class School { 8 @Value("清华大学") 9 private String name; 10 @Value(value = "北京大兴") 11 private String address; 12 13 public void setName(String name) { 14 this.name = name; 15 } 16 17 public void setAddress(String address) { 18 this.address = address; 19 } 20 21 @Override 22 public String toString() { 23 return "School{" + 24 "name='" + name + '\'' + 25 ", address='" + address + '\'' + 26 '}'; 27 } 28 }
2. 写spring主文件,使用组件扫描器
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" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> 6 <!--声明组件扫描器(component-scan),组件就是java对象--> 7 <context:component-scan base-package="com.bjpowernode.ba01"/> 8 </beans>
3.创建对象并使用
1 package com.bjpowernode; 2 3 import com.bjpowernode.ba01.Student; 4 import org.junit.Test; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 public class MyTest01 { 9 @Test 10 public void test01(){ 11 String path = "applicationContext.xml"; 12 ApplicationContext ac = new ClassPathXmlApplicationContext(path); 13 Student student = (Student) ac.getBean("myStudent"); 14 System.out.println(student); 15 } 16 }
package com.bjpowernode.ba01;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @component:创建对象的,等同于<bean/>的功能
* 属性:value 就是对象的名称,也就是bean的id值
* value的值是唯一的,创建的对象在整个spring容器中就一个
* 位置:在类的上面
* @component(value="myStudent")等同于
* <bean id="myStudent" class="com.bjpowernode.ba01.Student"/>
*
* spring中和@component功能一致,创建对象的注解还有
* 1. @Respository(用在持久层类的上面):放在dao的实现类上面,
* 标识创建dao对象,dao对象是能访问数据库的
* 2. @Service(用在业务层类的上面):放在service的实现类上面
* 创建service对象,service对象是做业务处理,可以有事务等功能的
* 3. @Controller(用在控制器的上面):放在控制器(处理器)类的上面,创建控制器对象的
* 控制器对象,能够接受用户提交的参数,显示请求的处理结果
* 以上三个注解的使用语法和@component一样的,都能创建对象,但是这三个注解还有额外的功能
* @Respository ,@Service,@Controller是个项目的对象分层的
*/
@Component(value = "myStudent")
public class Student {
/**
* @Value(value="")简单类型的属性赋值
* 1. 可以放到属性定义的上面,无需set方法
* 2. 在set方法的上面
*/
@Value(value = "张三")
private String name;
@Value(value = "22")
private Integer age;
/**
* @AutoWired对象属性的赋值(可以放到属性定义上面,也可以放到set方法的上面)
* 默认是byType的方式
* 如果要想使用byName方式,需要做的是
* 1. 在属性上面加入@AutoWired
* 2. 在属性上面加入@Qualifier(value="bean的id")
*/
@Autowired
@Qualifier("school")
private School school;
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", school=" + school +
'}';
}
}

浙公网安备 33010602011771号