基于注解的装配方式
对于依赖注入使用注解方式,将不再需要在Spring配置文件中声明Bean实例,具体方式如下
修改spring-context.xml
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <bean id="springContext" class="com.zck.my.shop.commons.context.SpringContext" /> <bean id="userService" class="com.zck.my.shop.service.impl.UserServiceImpl" /> <!--开启注解模式--> <context:annotation-config /> <context:component-scan base-package="com.zck.my.shop" /> </beans>
注解1:@componentt (该注解的value值为bean的id值)主要用于实例对象
Spring提供3个注解与component效果等效
@Repository:用于对DAO实现类注解
@Service:用于对Service实现类注解
@Controller:用于对Controller实现类进行注解
@Scope:(用于声明作用域,默认是单例模式)