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

采用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 可以根据属性的类型来注入工厂中存在的该类型的实例
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OOEM

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值