- @Component:标注一个普通的Spring Bean类。
- @Controller:标注一个控制器组件类。
- @Service:标注一个业务逻辑组件类。
- @Repository:标注一个Dao组件类。
另外两个常用的Spring Annotation:
- @Autowired:用来自动装配Bean,可以标注setter方法、普通方法、Field和构造器等。
- @Scope:用来标注一个Spring Bean的作用域,默认是singleton,对于action类我们会通常将其scope设置为prototype。
下面看如何使用Spring的annotation,首先需要在Spring的配置文件applicationContext.xml中添加如下配置:
<context:component-scan base-package="zwh.ssh.maven"></context:component-scan>
这里package属性要设置成你的组件所在的包,添加该语句后Spring会自动搜索该包和子包下的Bean组件。另外为了applicationContext.xml识别context的标签前缀,需要添加context的命名空间和其使用shema文件位置,其内容如下:
<?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-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- derby创建用户名和密码参考:http://www.joyzhong.com/archives/643 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>org.apache.derby.jdbc.EmbeddedDriver</value> </property> <property name="url"> <value>jdbc:derby:f:/zwh/mydb2</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>root</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.DerbyDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="annotatedClasses"> <list> <value>zwh.ssh.maven.po.User</value> </list> </property> </bean> <context:component-scan base-package="zwh.ssh.maven"></context:component-scan> </beans>
接下来给我的代码添加annotation,首先为dao添加:
@Repository("userDao") public class UserDaoDerbyImpl implements UserDao { @Autowired private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public List<User> findAllUser(){ Session session = this.sessionFactory.openSession(); List<User> list = session.createCriteria(User.class).list(); session.close(); return list; } }
这里的sessionFactory还是在xml文件里配置的,使用@Autowired来自动装配xml文件里的sessionFactory。并将该Dao类使用@Repository("userDao")标注,其id属性是userDao,自动装配时会根据该属性值将该对象注入进去。
为service添加annotation:
@Service("userService") public class UserServiceImpl implements UserService { Logger log = Logger.getLogger(UserService.class); @Autowired private UserDao userDao; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } public boolean login(String username,String password){ log.info("正在执行login方法...."); List<User> users =this.userDao.findAllUser(); for(User user : users){ if(user.getUsername().equals(username)){ if(user.getPassword().equals(password)){ return true; } else { return false; } } } return false; } public boolean check(String username) { List<User> users =this.userDao.findAllUser(); for(User user : users){ if(user.getUsername().equals(username)){ return true; } } return false; } }
注意这里@Autowired标注的Field的是userDao,与前面@Repository里面的内容是一致的。
action的annotation:
@Scope("prototype") @Controller public class LoginAction implements Action { @Autowired private UserService userService; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public String execute(){ String pwdMD5 = DigestUtils.md5Hex(user.getPassword()); if(this.userService.login(user.getUsername(), pwdMD5)){ ActionContext.getContext().getSession().put("username", this.user.getUsername()); return SUCCESS; } return ERROR; } }
这里使用@Scope("prototype")来指定了该action是原型类型的,即每来一个请求都要new一个这个类的对象,而不是使用单例,使用单例时如果有多个请求同时请求该action就会出现错误。