Spring原始注解
Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文 件可以简化配置,提高开发效率。
- @Component 使用在类上用于实例化Bean
- @Controller 使用在web层类上用于实例化Bean
- @Service 使用在service层类上用于实例化Bean
- @Repository 使用在dao层类上用于实例化Bean
- @Autowired 使用在字段上用于根据类型依赖注入
- @Qualifier 结合@Autowired一起使用用于根据名称进行依赖注入
- @Resource 相当于@Autowired+@Qualifier,按照名称进行注入
- @Value 注入普通属性
- @Scope 标注Bean的作用范围
- @PostConstruct 使用在方法上标注该方法是Bean的初始化方法
- @PreDestroy 使用在方法上标注该方法是Bean的销毁方法
注意:
使用注解进行开发时,需要在applicationContext.xml中配置组件扫描,作用是指定哪个包及其子包下的Bean需要 进行扫描以便识别使用注解配置的类、字段和方法。
<?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"xmlns:p="http://www.springframework.org/schema/p"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:component-scan base-package="com"/>
<context:property-placeholder location="jdbc.properties"/>
</beans>
使用@Compont或@Repository标识UserDaoImpl需要Spring进行实例化。
@Service("service")
@Scope("singleton")
public class service {@Autowired@Qualifier("impl")Dao dao;@Value("12")int no;@Value("${jdbc.username}")String name;public service(Dao dao) {this.dao = dao;}public service() {}public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");service i = (service) applicationContext.getBean("service");i.save();}public void save() {System.out.println(name);dao.save();}public void setDao(Dao dao) {this.dao = dao;}
}
@Repository("impl")
@Scope("singleton")
public class ImplDao implements Dao {public void save() {System.out.println("saving");}public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");ImplDao i = (ImplDao) applicationContext.getBean("impl");i.save();}
}
Spring新注解
使用上面的注解还不能全部替代xml配置文件,还需要使用注解替代的配置如下:
-
非自定义的Bean的配置:
-
加载properties文件的配置:context:property-placeholder
-
组件扫描的配置:context:component-scan
-
引入其他文件:
-
@Configuration 用于指定当前类是一个 Spring 配置类,当创建容器时会从该类上加载注解
-
@ComponentScan 用于指定 Spring 在初始化容器时要扫描的包。 作用和在 Spring 的 xml 配置文件中 的 <context:component-scan base-package=“com.itheima”/>一样
-
@Bean 使用在方法上,标注将该方法的返回值存储到 Spring 容器中
-
@PropertySource 用于加载.properties 文件中的配置
-
@Import 用于导入其他配置类
@Configuration
@ComponentScan("com")
@Import(DataSourceConfiguration.class)
public class SpringConfiguration {@Testpublic void test(){ApplicationContext applicationContext=new AnnotationConfigApplicationContext(SpringConfiguration.class);service service=(service) applicationContext.getBean("service");service.save();javax.sql.DataSource dataSource=(javax.sql.DataSource)applicationContext.getBean("datasource");try {System.out.println(dataSource.getConnection());} catch (SQLException throwables) {throwables.printStackTrace();}}
}
@PropertySource("classpath:jdbc.properties")
public class DataSourceConfiguration {@Value("${jdbc.driver}")String clazz;@Value("${jdbc.url}")String url;@Value("${jdbc.username}")String user;@Value("${jdbc.password}")String password;@Bean("datasource")public ComboPooledDataSource testC3P0() throws Exception {ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();comboPooledDataSource.setDriverClass(clazz);comboPooledDataSource.setJdbcUrl(url);comboPooledDataSource.setUser(user);comboPooledDataSource.setPassword(password);return comboPooledDataSource;}
}