Spring注解
注解方式可以简化spring的IOC容器的配置!
使用注解步骤:
1)先引入context名称空间
xmlns:context="http://www.springframework.org/schema/context"
2)开启注解扫描
<context:component-scan base-package="com.loaderman.e_anno2"></context:component-scan>
3)使用注解
通过注解的方式,把对象加入ioc容器。
创建对象以及处理对象依赖关系,相关的注解:
@Component 指定把一个对象加入IOC容器
@Repository 作用同@Component; 在持久层使用
@Service 作用同@Component; 在业务逻辑层使用
@Controller 作用同@Component; 在控制层使用
@Resource 属性注入
总结:
1) 使用注解,可以简化配置,且可以把对象加入IOC容器,及处理依赖关系(DI)
2) 注解可以和XML配置一起使用。
package loaderman.e_anno; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { // 创建容器对象 private ApplicationContext ac = new ClassPathXmlApplicationContext("loaderman/e_anno/bean.xml"); @Test public void testExecuteAction() { // 从容器中获取Action UserAction userAction = (UserAction) ac.getBean("userAction"); userAction.execute(); } }
package loaderman.e_anno; import javax.annotation.Resource; import org.springframework.stereotype.Component; @Component("userAction") // 加入IOC容器 public class UserAction { @Resource(name = "userService") private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } public String execute() { userService.save(); return null; } }
package loaderman.e_anno; import org.springframework.stereotype.Component; // 把当前对象加入ioc容器 @Component("userDao") // 相当于bean.xml 【<bean id=userDao class=".." />】 public class UserDao { public void save() { System.out.println("DB:保存用户!!!"); } }
package loaderman.e_anno; import javax.annotation.Resource; import org.springframework.stereotype.Component; @Component("userService") // userService加入ioc容器 public class UserService { // 会从IOC容器中找userDao对象,注入到当前字段 /* * <bean id="" class=""> * <property name="userDao" ref="userDao" /> @Resource相当于这里的配置 * </bean> */ @Resource(name = "userDao") private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void save() { userDao.save(); } }
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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="loaderman.e_anno"></context:component-scan> </beans>
package loaderman.e_anno2; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { // 创建容器对象 private ApplicationContext ac = new ClassPathXmlApplicationContext("loaderman/e_anno2/bean.xml"); @Test public void testExecuteAction() { // 从容器中获取Action UserAction userAction = (UserAction) ac.getBean("userAction"); userAction.execute(); } }
package loaderman.e_anno2; import javax.annotation.Resource; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; //@Component("userAction") // 加入IOC容器 //@Component @Controller // 控制层的组件 public class UserAction { @Resource private UserService userService; public String execute() { userService.save(); return null; } }
package loaderman.e_anno2; // 把当前对象加入ioc容器 //@Component("userDao") // 相当于bean.xml 【<bean id=userDao class=".." />】 //@Component // 加入ioc容器的UserDao对象的引用名称, 默认与类名一样, 且第一个字母小写 //@Repository // 在持久层可以选择用这个注解 public class UserDao { public UserDao(){ System.out.println("UserDao.UserDao()"); } public UserDao(int id){ System.out.println("UserDao.UserDao(int id)" + id); } public void save() { System.out.println("DB:保存用户!!!"); } }
package loaderman.e_anno2; import javax.annotation.Resource; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; //@Component("userService") // userService加入ioc容器 //@Component @Service // 表示业务逻辑层的组件 public class UserService { // @Resource // 根据类型查找 【在容器中要确保该类型只有一个变量】 @Resource(name = "userDao") // 根据名称查找 private UserDao userDao; // 去容器中招UserDao类型的变量,找到后就赋值 public void save() { userDao.save(); } }
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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="loaderman.e_anno2"></context:component-scan> <bean id="userDao" class="loaderman.e_anno2.UserDao" scope="prototype"> </bean> </beans>
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!