itjavashuai 2020-06-25
<!--开启注解全局扫描--> <context:component-scan base-package="包名"/>
@Repository("userDao") public class UserDao { public void save(){ System.out.println("DAO中保存用户..."); } }
@Test public void demo1(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = (UserDao) applicationContext.getBean("userDao"); userDao.save(); }
Spring2.5引入使用注解去定义Bean
@Component 描述Spring框架中Bean
除了@Component外,Spring提供了3个基本和@Component等效的注解
@Repository 用于对DAO实现类进行标注
@Service 用于对Service实现类进行标注
@Controller 用于对Controller实现类进行标注
这三个注解是为了让标注类本身的用途清晰,Spring在后续版本会对其增强
@Value("...")
使用@Autowired进行自动注入
@Autowired 默认按照类型进行注入
Spring提供对JSR-250中定义@Resource标准注解的支持
jdk比较高的版本,引入这个就可以正常使用@Resource注解
@Resource和@Autowired注解功能相似,默认按照名称注入
/* @Autowired @Qualifier("userDao") */ @Resource(name = "userDao")
@Autowired + @Qualifier("userDao") = @Resource(name = "userDao")
@PostConstruct
初始化
@PreDestroy
单例范围有效(销毁)
@Component("bean1") public class Bean1 { @PostConstruct public void init(){ System.out.println("initBean..."); } public void say(){ System.out.println("say..."); } @PreDestroy public void destory(){ System.out.println("destoryBean..."); } }
@Test public void demo1(){ ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Bean1 bean1 = (Bean1)applicationContext.getBean("bean1"); bean1.say(); applicationContext.close(); }
运行结果:
initBean... say... destoryBean...
@Scope注解用于指定Bean的作用范围
@Component("bean2") @Scope("prototype") // 默认单例,这里设置为多例 public class Bean2 { }
@Test public void demo2() { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Bean2 bean1 = (Bean2)applicationContext.getBean("bean2"); Bean2 bean2 = (Bean2)applicationContext.getBean("bean2"); System.out.println(bean1 == bean2); // false }
<context:component-scan base-package="....">
包中所有的注解都被解析<context:annotation-config>
只解析属性的注解
实例:
<context:annotation-config/> <bean id="productService" class="com.imooc.demo3.ProductService"> <!-- <property name="productDao" ref="productDao"/> <property name="categoryDao" ref="categoryDao"/>--> </bean> <bean id="productDao" class="com.imooc.demo3.ProductDao"/> <bean id="categoryDao" class="com.imooc.demo3.CategoryDao"/>
public class ProductService { @Resource(name = "categoryDao") private CategoryDao categoryDao; @Resource(name = "productDao") private ProductDao productDao; /* public void setCategoryDao(CategoryDao categoryDao) { this.categoryDao = categoryDao; } public void setProductDao(ProductDao productDao) { this.productDao = productDao; }*/ public void save() { System.out.println("ProductService的save方法执行了..."); categoryDao.save(); productDao.save(); } }
public class ProductDao { public void save(){ System.out.println("ProductDao的save方法执行了..."); } }
public class CategoryDao { public void save(){ System.out.println("CategoryDao中的save方法执行了..."); } }
@Test public void demo1() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); ProductService productService = (ProductService) applicationContext.getBean("productService"); productService.save(); }
ProductService的save方法执行了...
CategoryDao中的save方法执行了...
ProductDao的save方法执行了...