常用注解
1、用于创建bean对象的注解
1.1、@Component
package com.ly.spring; import org.springframework.stereotype.Component; //@Component:不配置value属性时通过类名小写获取person //@Component(value = "pers"):只能通过配置的value属性值获取 //@Component("pers"):可省略value属性直接写value属性值 @Component("pers") public class Person { }
1.2、由@Component衍生出的注解
@Controller:一般用于创建表现层bean对象
@Service:一般用于创建业务层bean对象
@Repository:一般用于创建持久层bean对象
2、自动按照类型注入属性@Autowired
spring容器中一个该类型的bean也没找到时会报错
spring容器中只找到一个该类型的bean时会注入成功
spring容器中若找到多个该类型的bean时,会继续根据属性名去匹配容器中bean的id,若匹配上了唯一的一个则会注入成功,若还是没匹配到则会报错
package com.ly.spring.service.impl; import com.ly.spring.dao.IAccountDao; import com.ly.spring.service.IAccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class AccountServiceImpl implements IAccountService { @Autowired //accountDao为属性名,若找到多个该类型的bean则会继续根据此属性名去匹配 private IAccountDao accountDao; @Override public void save() { System.out.println("AccountServiceImpl --- save"); accountDao.save(); } }
3、用于注入数据的注解
3.1、@Qualifier注解,用于注入类的属性时不可以单独使用,需配合@Autowired注解
若spring容器中一个该类型的bean都没找到时会报错
若spring容器中只找到一个该类型的bean,则继续用@Qualifier注解的value属性值与找到的bean的id去匹配,若匹配到则注入成功,若没匹配到则报错
若spring容器中找到多个该类型的bean时,继续用@Qualifier注解的value属性值与找到的bean的id去匹配,若匹配到则注入成功,若没匹配到则报错
package com.ly.spring.service.impl; import com.ly.spring.dao.IAccountDao; import com.ly.spring.service.IAccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @Service public class AccountServiceImpl implements IAccountService { @Autowired @Qualifier(value = "accountDao2") private IAccountDao accountDao1; @Override public void save() { System.out.println("AccountServiceImpl --- save"); accountDao1.save(); } }
3.2、@Resource注解根据bean的id注入
需要引入javax.annotation.Resource
<dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.2</version> </dependency>
package com.ly.spring.service.impl; import com.ly.spring.dao.IAccountDao; import com.ly.spring.service.IAccountService; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class AccountServiceImpl implements IAccountService { //name属性指定spring容器中bean的id @Resource(name = "accountDao1") private IAccountDao accountDao; public void save() { System.out.println("AccountServiceImpl --- save"); accountDao.save(); } }
4、改变bean作用范围的注解@Scope
package com.ly.spring.service.impl; import com.ly.spring.dao.IAccountDao; import com.ly.spring.service.IAccountService; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service //@Scope(value = "prototype") //@Scope(value = "singleton") 单例,默认值 @Scope(value = "singleton") public class AccountServiceImpl implements IAccountService { @Resource(name = "accountDao1") private IAccountDao accountDao; public void save() { System.out.println("AccountServiceImpl --- save"); accountDao.save(); } }
相识是缘