【Spring】IOC案例从XML到注解

  • 本例不展示业务代码。仅展示跟spring的使用相关的代码。

基于XML的IOC案例

  • service 实现类
public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
}

  • dao 实现类
public class AccountDaoImpl implements AccountDao {
    private QueryRunner runner ;
    public void setRunner(QueryRunner runner) {
        this.runner = runner;
    }
}
  • spring配置文件
<?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.xsd">
        
    <bean id="accountService" class="com.wei.service.impl.AccountServiceImpl" >
        <property name="accountDao" ref="accountDao" />
    </bean>

    <bean id="accountDao" class="com.wei.dao.impl.AccountDaoImpl" >
  	    <!--根据set方法名决定-->
        <property name="Runner" ref="queryRunner" />
    </bean>

    <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype" >
    	<!--因为源码是ds,所以只能写ds-->
        <constructor-arg name="ds" ref="dataSource" />
    </bean>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring" />
        <property name="user" value="root" />
        <property name="password" value="root" />
    </bean>
</beans>




基于注解的IOC案例

  • 需要导入context名称空间。上例XML已经加入了。后面我删除了bean.xml,仍然可以运行。混合使用的话,就需要spring配置文件,所以还是留着吧!

  • service / dao 接口 实体类
public interface AccountService {
    //业务代码
}
public interface AccountDao {
    //业务代码
}
@Data
public class Account implements Serializable {
	//字段
}

  • service / dao 实现类
@Service("accountService")
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;
    //业务代码
}

@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private QueryRunner runner ;
    //业务代码
}

  • config 类, 主配置类、子配置类、jdbcConfig.properties
@Configuration
@ComponentScan(basePackages = "com.wei")
@Import({JdbcConfig.class})
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {
}


@Configuration
public class JdbcConfig {
	//需要读取配置文件,读取的注解在主配置文件上(PropertySource)
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean(name = "runner")
    public QueryRunner createQueryRunner(@Qualifier("dataSource") DataSource dataSource){
        return new QueryRunner(dataSource);
    }
    @Bean("dataSource")
    @Scope("prototype")
    public DataSource createDataSource(){
        ComboPooledDataSource ds = new ComboPooledDataSource();
        ds.setDriverClass(driver);ds.setJdbcUrl(url);ds.setUser(username);ds.setPassword(password);
        return ds;
    }
}
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=root

Spring 整合 Junit

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( classes = SpringConfiguration.class)
public class Client {
    @Autowired
    private AccountService accountService;
    
    @Test
    public void findAll(){
        List<Account> all = accountService.findAll();
        System.out.println(all);
    }
}

pow.xml

<packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.40</version>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

nickkkkkkkkk

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

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

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

打赏作者

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

抵扣说明:

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

余额充值