一、@Component 、@Respository、@Service、@Controller
(1)@Component 把普通java类创建对象放入Spring容器中
相当于 xml 配置文件中的<bean id="" class="">...</bean>
例如:
public class Cat {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myCat" class="com.gushi.web.Cat">
<property name="name" value="汤姆猫"/>
<property name="age" value="3"/>
</bean>
</beans>
在类上面使用了 @Component
注解就不用写 xml 配置文件了。
@Component("myCat")
public class Cat {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
(2)@Respository 和 @Component 类似,分类不同, 作用于持久层对象, 表示对象能访问数据库。
(3)@Service 和 @Component 类似,分类不同,作用于业务层对象, 处理业务逻辑,具有事务能力。
(4)@Controller 和 @Component 类似,分类不同,作用于控制器对象(视图层对象), 接收请求,显示请求的处理结果。
二、@Value 将配置文件中的值注入到变量中
配置文件
school.name=天明
school.age=13
Java类
public class Student {
@Value("school.name") // 将配置文件中 school.name 的值赋给了 name
private String name;
@Value("school.age") // 将配置文件中 school.age 的值赋给了 age
private Integer age;
}
三、@Configuration @Bean
Spring 使用 xml 作为容器配置文件, 在 3.0 以后可以使用 java 类做配置文件使用。
使用Java类做配置文件会用到两个注解:@Configuration
、@Bean
@Configuration
:放在类的上面,表示这个类是作为配置文件使用的,可替换xml配置文件
@Bean
:放在方法的上面,声明对象,并把对象注入到容器中,主要用在@Configuration
注解的类里。如果不指定对象名称,在容器中,默认id是方法名。
创建数据类 Student
package com.gushi.myannotation.domain;
public class Student {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
使用 xml 作为容器配置文件,在 resources 目录下创建 Spring 的 xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--声明bean对象-->
<bean id="myStudent" class="com.gushi.myannotation.domain.Student">
< property name="id" value="1001" />
< property name="name" value="天明" />
< property name="age" value="15" />
</bean>
</beans>
使用 类 作为容器配置文件
@Configuration
public class MyConfig {
@Bean("myStudent")
public Student creatStudent(){
Student student = new Student();
student.setId(1001);
student.setName("天明");
student.setAge(15);
return student;
}
}
使用单元测试
@Test
public void xmlTest(){
String config = "applicationContext.xml"; // 在 resources 目录下创建的 Spring 配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
Student student = (Student)ctx.getBean("myStudent");
System.out.println("xml作为配置文件创建的bean对象" + student);
}
@Test
public void configTest(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfig.class);
Student student = (Student)ctx.getBean("myStudent");
System.out.println("使用类作为配置文件创建的bean对象" + student);
}
四、@ImportResource
@ImportResource
:导入 xml 配置,等同于 xml 文件的 resources
<import resources="其他配置文件"/>
创建数据类 Dog
public class Dog {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
使用 xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myDog" class="com.gushi.myannotation.domain.Dog">
<property name="id" value="1001" />
<property name="name" value="旺财"/>
<property name="age" value="3" />
</bean>
</beans>
创建配置类
@Configuration
// 使用 classpath 指定类路径,是编译后的,在target下的classes下
@ImportResource(value = "classpath:applicationContext.xml")
public class MyConfig {
}
}
使用 @ImportResource
就把 xml 中的 bean 导入了容器中,参数里的 classpath
指的类路径,是编译后的,用IDEA是在target下的classes下。
@ImportResource(value = " ")
的参数value
的源码如下:
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.core.annotation.AliasFor;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
public @interface ImportResource {
@AliasFor("locations")
String[] value() default {};
@AliasFor("value")
String[] locations() default {};
Class<? extends BeanDefinitionReader> reader() default BeanDefinitionReader.class;
}
可以看到是个String
的数组,如果有多个文件,可以这么写:
@ImportResource(value = {"classpath:a.xml","classpath:b.xml","classpath:c.xml..."})
五、@PropertyResource
@PropertyResource
:读取 properties 属性配置文件,使用属性配置文件可以在程序代码之外提供数据,实现外部化配置。修改相关数据可以不修改源码,只改配置文件即可。
创建数据类 Cat
public class Cat {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Cat{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
在IDEA的resources目录下,创建properties文件, 在properties文件中使用k=v的格式提供数据。
cat.id=1001
cat.name=Tom
cat.age=3
在数据类中使用相关注解
@Component("cat")
public class Cat {
@Value("${cat.id}")
private Integer id;
@Value("${cat.name}")
private String name;
@Value("${cat.age}")
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Cat{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
在配置类中加入相关注解
@Configuration
@PropertySource(value = "classpath:config.properties")
@ComponentScan(basePackages = "com.gushi.myannotation.domain")
public class MyConfig {
}
}