文章目录
将已学到的spring注解和xml配置进行了比较。
用于将类创建为对象的注解
这一类注解通常可以取代XML配置文件中的:
<bean id="" class="" scope="" init-method="" destroy-method="">
<property name="" value=""|res=""></property>
</bean>
Component
作用:用于把当前类对象存入spring容器
属性:
value:用来指定容器中bean的id属性,默认为类名首字母小写的类名
@Component(value="accountService")
public class AccountService{
}
Controller
一般用在表现层,用法同Component.
Service
一般用在业务层,用法同Component.
Repoeitory
一般用在持久层,用法同Component.
用于注入数据的注解
作用和xml配置文件中的bean标签写一个功能一样。
其中Autowired、Qualifier和Resource只能用于注入其他bean类型的数据,Value只能注入基本类型和String类型,集合类型 注入只能通过XML配置的方式注入。
Autowired
作用:自动按照类型注入,只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功。如果没有找到(根据类型在容器的value中查找对应类型)则报错。如果有多个匹配时,会根据变量名称匹配(与容器的bean key匹配),若无法匹配,则报错。可以用在变量和方法上。此时,set方法不是必须的。
@Service("accountService")
public class AccountService{
@Autowired
private IAccountDao accountDao;
}
Qualifier
作用:在按照类中注入的基础上再按照名称注入,给类成员注入时不能单独使用。但是在给方法参数上注入时可以单独使用。
属性:
value:用于指定bean的id,也可省略value。
@Service("accountService")
public class AccountService{
@Autowired
@Qualifier(value="accountDao")
private IAccountDao accountDao;
public void myFun(@Qualifier("accountDao") IAccountDao accountDao){
}
}
Resource
作用:直接按照bean的id注入,它可以独立使用。
属性:
name:用于指定bean的id。
@Service("accountService")
public class AccountService{
@Resource(name="accountDao")
private IAccountDao accountDao;
}
Value
作用:用于注入基本类型和String类型
属性:
value:用于指定数据的值,它可以使用spring中的SqEL(也就是spring的el表达式)。写法: ${表达式}
@Service("accountService")
public class AccountService{
@Value("lhw")
private String name;
}
用于作用范围的注解
作用就和bean标签中使用scope属性实现的功能相同.
Scope
作用:用于指定bean的作用范围
属性:
value:指定范围的取值,singleton、prototype,默认单例.
@Service("accountService")
@Scope(value="singleton")
public class AccountService{
}
用于生命周期的注解
作用就和在bean标签中使用init-method和destroy-method实现功能相同。
PostConstruct
作用:用于指定初始化方法
@Service("accountService")
public class AccountService{
@PostConstruct
public void init(){
}
}
PreDestroy
作用:用于指定销毁方法。
@Service("accountService")
public class AccountService{
@PreDestroy
public void destory(){
}
}
与配置类相关的注解
配置类,作用和bean.xml相同。使用过程中无法将注解加入到jar包中,只能通过XML配置或者注解类的方式将对象加入spring容器中。通过AnnotationConfigApplicationContext获得spring容器。
ApplicationContext ac=new AnnotationConfigApplicationContext();
Configuration
作用:指定当前类是注解类
当配置类作为AnnotationConfigApplicationContext的参数时,Configuration注解可用省略。
@Configuration
public class SpringConfig{
}
ComponentScan
作用:用于通过注解指定spring创建容器所扫描的包。
属性:
value 和 basePackages一样,等同于在xml中配置了。
XML配置:
<context:component-scan base-package=" "></context:component-scan>
注解配置:
@Configuration
@ComponentScan(basePackages=" ")//包路径
public class SpringConfig{
}
Bean
作用:用于把当前方法的返回值作为bean对象存入容器中。
属性:
name 用于指定bean的id,默认为方法名
当使用注解配置时,如果方法有参数,spring框架会去容器中查找有没有可用的bean对象。查找的方式和Autowired注解一样
@Configuration
@ComponentScan(basePackages=" ")//包路径
public class SpringConfig{
@Bean(name="date")
public Date getDate(){
return new Date();
}
}
Import
作用:用于导入其他的配置类。
属性:
value:用于指定其他类的字节码,当使用Import注解的类是父配置类,导入的类都是子配置类。
@Configuration
@ComponentScan(basePackages=" ")//包路径
@Import(jdbcConfig.class)
public class SpringConfig{
}
PropertySource
作用:用于指定properties文件的名称和路径,classpath表示类路径下。
@Configuration
@ComponentScan(basePackages=" ")//包路径
@Import(jdbcConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfig{
}
JdbcConfig配置类
public class JdbcConfig{
@Value("${jdbc.driver}")
private String driver;
}
jdbcConfig.properties文件
jdbc.driver=com.mysql.cj.jdbc.Driver