javascript
Spring常用的的注解对应xml配置详解
@Component(value="")注解:組件
相當于XML配置文件中的Bean標簽
作用與@Component注解一樣
設計初衷增加代碼的可讀性,體現在三層架構上
@Controller一般標注在表現層
@Service一般標注在業務層
@Repository一般 標注在持久層
注意:此注解必須搭配掃描注解使用
@Configuration @ComponentScan("com.*") public class SpringConfig{}或 XML配置
<context:component-scan base-package="com.*"></context:component-scan>進行注解掃描。
?
@Autowired注解:byType自動注入
默認按照變量數據類型注入,如果數據類型是接口,注入接口實現類。
相當于XML配置文件中的property標簽
成員變量的接口數據類型,有多個實現類的時候,要使用bean的id注入,否則會報錯。
Spring框架提供的注解
必須指定Bean的id,使用@Qualifier的注解配合,@Qualifier注解的value屬性指定bean的id
舉例
@Component("userAnnonService02") public class UserAnnonServiceImpl01 implements UserAnnonService {} @Component("userAnnonService01") public class UserAnnonServiceImpl02 implements UserAnnonService {}使用需要注意,因為一個接口被兩個實現類實現,所以根據屬性已經失效了使用@Qualifier選擇要注入的實現類
public class Test{ @Autowired @Qualifier("userAnnonService01") UserAnnonService userAnnonService; }其他補充
<bean id="userService" class="com.spring.service.UserServiceImpl"> <property name="userMapper" ref="userMapper"></property> </bean> <bean id="userMapper" class="com.spring.mapper.UserMapperImpl"></bean>等價于注解開發的
@Component("userService") public class UserServiceImpl implements UserService { @Autowired UserAnnonMapper userAnnonMapper; }@Component("userAnnonMapper") public class UserMapperImpl implements UserMapper {}這里是我認為初學者比較容易搞混的地方。
?
@Resource(name="") byName注解(jdk提供)
相當于XML配置文件中的property標簽
?
@Configuration注解
標記在類上
注解作用:作用等同于beans.xml配置文件,當前注解聲明的類中,編寫配置信息,所以我們把
@Configuration聲明的類稱之為配置類。
@Import注解
標記在類上
注解作用:導入其他配置類(對象)
相當于XML配置文件中的標簽
引用場景
在配置文件按配置項目時使用xml分層
例如 mvc.xml,dao.xml,service.xml分別配置提高可讀性和可維護性,使用 import 引入同一個xml中進行讀取。
多個configuration配置類時使用
@Import(配置類.class) 或 @Import({配置類.class,配置類.class}) 參數為數組加載多個配置類
?
@PropertySource注解
標記在類上
注解作用:引入外部屬性文件(db.properties)
相當于XML配置文件中的context:property-placeholder標簽
或者
@PropertySource("classpath:db.properties")@Value注解
標記在成員變量或set方法上
注解作用:給簡單類型變量賦值
相當于XML配置文件中的標簽
@Bean注解
標記在配置類中的方法上
注解作用:將方法的返回值存儲到Spring IOC容器中
相當于XML配置文件中的標簽
舉例 @PropertySource @Value @Bean 的整合
@Configuration @PropertySource({"db.properties"}) public class SpringConfigClass { @Value("${jdbc.driver}") private String dataSource; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String userName; @Value("${jdbc.password}") private String passWord; @Bean public DruidDataSource dataSource() { DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(dataSource); druidDataSource.setUrl(url); druidDataSource.setUsername(userName); druidDataSource.setPassword(passWord); return druidDataSource; } }等價于 xml
<context:property-placeholder location="classpath*:db.properties"/> <!--注入 druid--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean>@ComponentScan(“com.*”)注解
標記在配置類上
相當于XML配置文件中的context:component-scan標簽
屬性
value:指定要掃描的package; 若value值為空則掃描當前配置類的包以及子包。
includeFilters=Filter[]:指定只包含的組件
excludeFilters=Filter[]:指定需要排除的組件;
useDefaultFilters=true/false:指定是否需要使用Spring默認的掃描規則:被@Component, @Repository, @Service, @Controller或者已經聲明過@Component自定義注解標記的組件;
在過濾規則Filter中:
FilterType:指定過濾規則,支持的過濾規則有
ANNOTATION:按照注解規則,過濾被指定注解標記的類;
ASSIGNABLE_TYPE:按照給定的類型;
ASPECTJ:按照ASPECTJ表達式;
REGEX:按照正則表達式
CUSTOM:自定義規則;
value:指定在該規則下過濾的表達式;
舉例
// useDefaultFilters = false 關閉默認過濾使用創建的過濾 // value = {UserAllAnnonService.class, UserAllAnnonMapper.class} 只掃描這兩個接口的組件注解 @Configuration @ComponentScan(includeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {UserAllAnnonService.class, UserAllAnnonMapper.class})},useDefaultFilters = false) public class SpringConfigClass { }?
@EnableAspectJAutoProxy 開啟Aop注解支持
對應標簽
aop:aspectj-autoproxy/
舉例
@EnableTransactionManagement 開啟注解事務控制
對應xml標簽
<tx:annotation-driven/> @Configuration @ComponentScan @PropertySource("classpath:sqlLink.properties") @ImportResource({"classpath:beans.xml"}) @EnableTransactionManagement public class SpringConfigClass {...}@Transactional() 事務處理,加到方法上,開啟當前方法的事務支持
常用屬性
transactionManager 屬性: 設置事務管理器,如果不設置默認是transactionManager。
isolation屬性: 設置事務的隔離級別。
propagation屬性: 事務的傳播行為。
?
其他注解
@Scope(“prototype”)
標記在類上,配合@Component使用
注解作用:指定對象的作用范圍:單例模式(singleton)還是多例模式(prototype)
?
@PostConstruct、@PreDestroy 生命周期注解
@PostConstruct ==> init-method
舉例:
@PreDestroy ==> destroy
舉例:?
?
@ImportResource({“classpath:beans.xml”})
引入外部配置,此注解適用于配置類和xml配置共同存在
舉例
對應xml配置
<import resource="beans.xml"></import>@Aspect 切面
注解作用:當前類的對象,是一個切面類
<aop:config><aop:aspect id="" ref="" /></aop:config>@Pointcut 切點
@Pointcut("execution(public void com.itheima.service.AccountServiceImpl.save())") public void pointcut() {}對應xml
<aop:pointcut id="pointcutService" expression="execution(* com.spring.service.UserServiceImpl.*(..))"/>@After(“pointcut()”) 后置通知
@AfterThrowing(“pointcut()”) 異常通知
@AfterReturning(“pointcut()”) 最終通知
@Before(“pointcut()”) 前置通知
舉例
對應xml
<aop:before method="beforePrintLog" pointcut-ref="pointcutService"/>@Around(“pointcut()”) 環繞通知
//要求必須要傳遞一個參數: ProceedingJoinPoint @Around("pointcut()") public void aroundPrint(ProceedingJoinPoint joinPoint) {}@MapperScan
指定要變成實現類的接口所在的包,然后包下面的所有接口在編譯之后都會生成相應的實現類
@MapperScan("com")?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Spring常用的的注解对应xml配置详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第一百三十八期:37 个MySQL数据库
- 下一篇: 刻录DVD视频光盘