Spring第六讲学习笔记,使用注解开发。Repository,Service,Controller
2017-06-14 20:44
676 查看
把spring中user模块的user.xml配置文件中,之前为UserAction配置userService的时候,struts的配置文件
中的class可以填这个bean的id,现在可以把这种方法干掉。仍然使用以前struts的方法,
class="action.UserAction",而不是bean的ID。这还是让struts来管理action。
在struts的配置文件中,让struts创建userService对象,struts的配置文件中class写
Action.UserAction。这样为注解开发提供了条件。
使用注解开发,
1.修改Spring头文件,增加context。
省去Spring模块的user.xml中UserDao中sqlSessionFactory,
UserService中的userDao,
UserAction中的userService的bean注入
2.在Spring配置文件中写
<context:component-scan base-package="dao.imp"/>
<context:component-scan base-package="service.imp"/>
<context:component-scan base-package="action"/>
这个时候,突然发现如果包名前缀是相同的,如cn.sxt....,那么这里就能直接写
<context:component-sacn base-package:"cn.sxt"/>
3.在UserDaoImpl中自动注入sqlSessionFactory
在UserDao的实现类的上一行写
@Repository("userDao") :数据仓库,相当于<bean id="userDao" class="实现类">,
这里是在实现类里,@Repository括号里的相当于id,
可以通过beanFactory.getBean("userDao")拿到UserDao接口的实现类。
接下来自动注入sqlSessionFactory字段,调用父类的setSqlSessionFactory方法,
在set方法上写@Autowired以便自动注入,要么在字段上写。
4.在UserServiceImpl中自动注入userDao
在类的上一行写@Service("userService"),如果填写括号里的userService,那么默认是类名,
就是@Service("UserServiceImpl")
在userDao上写@Autowired,当然因为是自动注入,所以必须要有get和set方法
5.在Action中自动注入userService
在类的上一行写@Controller("userAction"),说白了,括号就是给类添加一个标签,
在测试类中的beanFactory.getBean("userAction")能够获取到对应类的实例对象。
在userService上写@Autowired,为userService自动注入,同理必须有set和get方法
中的class可以填这个bean的id,现在可以把这种方法干掉。仍然使用以前struts的方法,
class="action.UserAction",而不是bean的ID。这还是让struts来管理action。
在struts的配置文件中,让struts创建userService对象,struts的配置文件中class写
Action.UserAction。这样为注解开发提供了条件。
使用注解开发,
1.修改Spring头文件,增加context。
省去Spring模块的user.xml中UserDao中sqlSessionFactory,
UserService中的userDao,
UserAction中的userService的bean注入
2.在Spring配置文件中写
<context:component-scan base-package="dao.imp"/>
<context:component-scan base-package="service.imp"/>
<context:component-scan base-package="action"/>
这个时候,突然发现如果包名前缀是相同的,如cn.sxt....,那么这里就能直接写
<context:component-sacn base-package:"cn.sxt"/>
3.在UserDaoImpl中自动注入sqlSessionFactory
在UserDao的实现类的上一行写
@Repository("userDao") :数据仓库,相当于<bean id="userDao" class="实现类">,
这里是在实现类里,@Repository括号里的相当于id,
可以通过beanFactory.getBean("userDao")拿到UserDao接口的实现类。
接下来自动注入sqlSessionFactory字段,调用父类的setSqlSessionFactory方法,
在set方法上写@Autowired以便自动注入,要么在字段上写。
@Repository("userDao") public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{ @Autowired @Override public voidsetSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { // TODO Auto-generated method stub super.setSqlSessionFactory(sqlSessionFactory); } |
在类的上一行写@Service("userService"),如果填写括号里的userService,那么默认是类名,
就是@Service("UserServiceImpl")
在userDao上写@Autowired,当然因为是自动注入,所以必须要有get和set方法
@Service("userService") public class UserServiceImpl implements UserService{ @Autowired private UserDao userDao; |
在类的上一行写@Controller("userAction"),说白了,括号就是给类添加一个标签,
在测试类中的beanFactory.getBean("userAction")能够获取到对应类的实例对象。
在userService上写@Autowired,为userService自动注入,同理必须有set和get方法
@Controller("userAction") @Scope("prototype") public class UserAction implements Action{ @Autowired private UserService userService; |
相关文章推荐
- 使用spring注解@Controller @Service @Repository简化配置
- SpringMVC学习笔记(三)使用IntelliJ IDEA开发Spring MVC HelloWorld(注解配置SpringMVC)
- 使用spring注解@Controller @Service @Repository简化配置
- Spring注解@Componen、@Repository@Service、@Controller区别与Spring2.5中使用注解装配属性and组件自
- 使用spring注解@Controller @Service @Repository简化配置
- Spring学习使用标签来标记资源(@Component、@Repository、 @Service和@Controller)和用法(包括如何jsp正在使用)
- spring 学习笔记 使用pojo+xml的方式开发aop
- Spring学习笔记(14)----使用Spring的注解方式实现AOP
- iPhone开发学习笔记005——使用XIB自定义一个UIView,然后将这个view添加到controller的view
- Spring注解:@Repository、@Service、@Controller、@Autowired
- Spring注解 Component Repository Service Controller区别
- 【Java EE 学习 24 下】【注解在数据库开发中的使用】【反射+注解+动态代理在事务中的应用service层】
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP的细节)
- Spring注解(Repository、Service、Controller)
- 斯坦福大学公开课 iOS应用开发教程学习笔记(第六课)多个MVC的程序和故事版、UINavigationController、 Segues
- Spring中注解@Controller@Service@Repository@Component的区别
- @Component @Service @Controller @Repository注解使用
- Spring2.5注解(标注)学习笔记(使用annotation代替XML)
- spring学习笔记7--使用spring进行面向切面的(AOP)编程(1)注解方式实现
- spring学习笔记10--Spring+jdbc组合开发(注解方式配置事务)