Spring原始注解
Spring是輕代碼而重配置的框架,配置比較繁重,影響開發效率,所以注解開發是一種趨勢,注解代替xml配置文 件可以簡化配置,提高開發效率。
- @Component 使用在類上用于實例化Bean
- @Controller 使用在web層類上用于實例化Bean
- @Service 使用在service層類上用于實例化Bean
- @Repository 使用在dao層類上用于實例化Bean
- @Autowired 使用在字段上用于根據類型依賴注入
- @Quali?er 結合@Autowired一起使用用于根據名稱進行依賴注入
- @Resource 相當于@Autowired+@Quali?er,按照名稱進行注入
- @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
-
引入其他文件:
-
@Con?guration 用于指定當前類是一個 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;}
}