Spring 注解@Service @Autowired 对应xml配置介绍

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_23315711/article/details/82876828

采用Spring 注解方式表达

Service 层

public interface UserService{
    
    boolean login(String username,String password,String ip);
    
}

Service implementl 层

  • 在这里新增了两个注解 @Service、@Autowired ,减少了getter和setter
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private AccountAccessDao accountAccessDao;
    @Autowired
    private LoginLogAccessDao loginLogAccessDao;
    
    public boolean login(String username, String password,String ip) {
        
        if (!accountAccessDao.isAccountExist(username)) {
            return false;
        }
        
        if (accountAccessDao.isPasswordRight(username, password)) {
            accountAccessDao.updateLastLoginTime(username);
            loginLogAccessDao.addLoginLog(username, ip);
            return true;
        }
        return false;
    }
}

Spring xml 文件配置对比

  • @Service 在类前注释,将此类实例化,等同于在spring-context中配置
 <bean class="com.milosun.demo.service.UserServiceImpl"></bean>
  • @Autowired 在属性前配置,将属性对应的对象从工厂中取出并注入到该bean中,等同于
  <property ref="accountAccessDaoImpl" name="accountAccessDaoImpl"></property>
  <property ref="loginLogAccessDaoImpl" name="loginLogAccessDaoImpl"></property>
  • 两个注解组合起来等同于:
<bean class="com.milosun.demo.service.UserServiceImpl">
    <property ref="accountAccessDaoImpl" name="accountAccess"></property>
    <property ref="loginLogAccessDaoImpl" name="loginLogAccess"></property>
</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.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <bean id="accountAccessDaoImpl" class="com.milosun.demo.dataaccess.AccountAccessDaoImpl"></bean>
    <bean id="loginLogAccessDaoImpl" class="com.milosun.demo.dataaccess.LoginLogAccessDaoImpl"></bean>
    
    <context:component-scan base-package="com.milosun.demo.service"></context:component-scan>

</beans>
  • 当然是可以的了,配置com.milosun.demo.service.UserServiceImpl这个bean的代码被两个注解代替了,而新增了一行
<context:component-scan base-package="com.milosun.demo.service"></context:component-scan>
  • com.milosun.service包下面扫描 componet 而@Service正是componet 中的一种。

然后我们再优化一下上面配置,毕竟spring-context.xml 配置文件还有两行<bean>元素。

步骤:

1.删除spring-context,xml中的两行<bean>元素

2..将AccountAccessDaoImpl.java和LoginLogAccessDaoImpl.java的类定义前增加@Service注解

3.修改spring-context.xml 中componet-scan的 base-package属性为com.milosun.demo,将会扫描包名路径下所有注解,如下所示

<context:component-scan base-package="com.milosun.demo"></context:component-scan>

总结:

  1. @Service可以将一个类定义成一个bean(也就是实例化并放入工厂)
  2. @Autowired 可以根据属性的类型来注入工厂中存在的该类型的实例

猜你喜欢

转载自blog.csdn.net/qq_23315711/article/details/82876828