Java日志三十四「基于注解的Ioc配置」

**

基于注解的Ioc配置

**
上一篇大概讲了一下spring的IoC的概念和用法,这一篇我们用注解加xml的方式来简化我们的开发。
首先我们使用注解的目的就是简化xml文件的书写。首先,我们先把xml文件中最重要的部分替代。就是把对象注入容器。这里有四个注解供我们使用
@Controller: 一般用于表现层的注解。
@Service: 一般用于业务层的注解。
@Repository: 一般用于持久层的注解。
@Component。
这四个注解作用都是一样的,但是建议使用要有规范。
使用注解配置把对象加载入容器

@Service("accountService")
public class AccountServiceImpl implements AccountService { 
}

这样就结束了!
我们在看一下之前的xml文件配置

<bean id="accountService" class="Gao.service.impl.AccountServiceImpl">
</bean>

不仅简单了很多,xml文件可读性也变的更高。

但是只能把对象加载进容器是不够的,我们在XML文件里面还要注入对象的成员变量,一般配置如下

<bean id="accountService" class="Gao.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>

我现在通过注解的方式注入AccountDao对象

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;
}

这样就把AccountDao对象给注入进来了,比之前的xml文件配置简单了很多。

现在我们看一个完整的查询

持久层代码

@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {

    @Autowired
    private QueryRunner runner;

    public List<Account> findAllAccount() {
        try{
            return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}

业务层代码

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;
    
	public List<Account> findAllAccount() {
        return accountDao.findAllAccount();
    }
    
}

bean.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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
<!--    告知spring在创建容器时要扫描的包-->
    <context:component-scan base-package="Gao"></context:component-scan>
<!--    配置QueryRunner对象-->
    <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<!--        注入数据源-->
        <constructor-arg name="ds" ref="dataSource"></constructor-arg>
    </bean>
<!--    配置数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--        注入连接数据库的必备信息-->
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy_spring"></property>
        <property name="user" value="root"></property>
        <property name="password" value="000222abc"></property>
    </bean>
</beans>

单元测试

public class AccountServiceTest {

    @Test
    public void testFindAll() {
        ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
        AccountService as=ac.getBean("accountService",AccountService.class);
        List<Account> accounts=as.findAllAccount();
        for(Account account:accounts){
            System.out.println(account);
        }
    }
}

结果

Account{id=1, name='aaa', money=1000.0}
Account{id=2, name='bbb', money=1000.0}
Account{id=3, name='ccc', money=3000.0}
Account{id=5, name='ddd', money=1000.0}
Account{id=6, name='eee', money=5000.0}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值