Spring(二):IOC注解开发

Spring(二):IOC注解开发

1. spring基于XML的CRUD案例

1.1 环境搭建
  • 创建maven项目,导入坐标
<dependencies>
    <!--springIOC相关-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
	<!--spring整合Junit-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.1.3.RELEASE</version>
    </dependency>
	<!--mysql驱动-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.26</version>
    </dependency>
    <!--druid连接池-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.0.9</version>
    </dependency>
	<!--jdbcTemplate相关-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.0.0.RELEASE</version>
    </dependency>
    <!--junit测试-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>RELEASE</version>
    </dependency>
</dependencies>
  • 数据库和实体类

public class Account {
    private Integer accountId;
    private Integer uid;
    private Double money;
}
  • dao层增删改查代码(接口为IAccountDao,这里省略不写)
package com.zmysna.dao.impl;

public class AccountDaoImpl implements IAccountDao {
	
    private JdbcTemplate jdbcTemplate;

    public void saveAccount(Account account) {
        jdbcTemplate.update("INSERT INTO account values(?,?,?)",
                account.getAccountId(), account.getUid(), account.getMoney());
    }

    public void updateAccount(Account account) {
        jdbcTemplate.update("UPDATE account SET uid=?, money=? where accountId=?",
                account.getUid(), account.getMoney(), account.getAccountId());
    }

    public Account findAccountById(int id) {
        try {
            return jdbcTemplate.queryForObject("select * from account where accountId = ?", new BeanPropertyRowMapper<>(Account.class), id);
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void deleteAccount(int id) {
        jdbcTemplate.update("DELETE FROM account where accountId = ?", id);
    }
}
  • service层增删该查代码(接口为IAccountService,这里省略不写)
public class AccountServiceImpl implements IAccountService{

    private IAccountDao accountDao;

    public Account getAccountById(int accountId) {
        Account accountById = accountDao.findAccountById(accountId);
        return accountById;
    }

    public void updateAccount(Account account) {
        accountDao.updateAccount(account);
    }

    public void deleteAccount(int accountId) {
        accountDao.deleteAccount(accountId);
    }

    public void saveAccount(Account account){
        accountDao.saveAccount(account);
    }
}
1.2 spring容器配置,管理所有的java类
  • jdbc信息
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8
jdbc.username=root
jdbc.password=awsed2zx
jdbc.initialSize=3
jdbc.maxActive=50
  • applicationContext.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.xsd">
	<!--加载jdbc配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
   <!--配置数据库连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
	<!--配置jdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg ref="dataSource" name="dataSource"></constructor-arg>
    </bean>
	<!--配置dao层-->
    <bean id="accountDao" class="com.zmysna.dao.impl.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
	<!--配置service层-->
    <bean id="accountService" class="com.zmysna.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>

</beans>
1.3 测试
@Test
public void getAccountTest() {
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    IAccountService accountService = ac.getBean("accountService", IAccountService.class);
    System.out.println(accountService.getAccountById(1));
}

###2. spring常用注解介绍

2.1 创建对象加入容器

​ 使用@Component(“bean名称”)可以配置对象的名称

以下四个注解的功能都是相同的, 为了更好的标识不同的java类使用不同的注解

@Component 用于工具类、其他组件

@Component
public class Account {}

@Repository 标识数据库访问层的组件

@Repository("accountDao") //创建dao层对象加入容器
public class AccountDaoImpl implements IAccountDao {}

@Service 标识业务逻辑层的组件

@Service("accountService") //创建service层对象加入容器
public class AccountServiceImpl implements IAccountService{}

@Controller 标识控制层的组件

2.2 依赖注入
  • @Autowired

    • 从容器中找对象,给属性赋值。根据类型、名称去容器中找。
    1. 注入到属性上面
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    1. 注入到方法形参中(方法名可以随意定义)
    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    
  • @Qualifier

    • 结合 Autowired 使用,可以指定按照名称去容器找对象注入。
    @Autowired
    @Qualifier("jdbcTemplate") //根据"jdbcTemplate"这个名称到容器中寻找对象
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    
  • @Value

    • 给简单类型属性直接赋值/获取配置文件值@Value(“${key}”)
    1. 简单类型属性
    @Value("11")  //给简单类型属性赋值
    private int i;
    
    1. 获取配置文件值

      1. 配置文件
      jdbc.username=root
      
      1. 赋值
      @Value("${jdbc.username}")
      private String username;
      
  • @Resource

    从容器中找对象,给属性赋值。 根据名称、类型去容器中查找。和 Autowired 功能类似,查找对象的顺序是想查找名称,再查找类型,和Autowired相反。只能在 jdk1.5~1.8 使用, 不推荐使用。

####2.3 作用范围

@Scope:和bean标签中的scope属性功能相同

设置bean的作用范围,取值如下:
singleton:单例,默认值
prototype:多例
request:web项目中,将对象存入request域中
session: web项目中,将对象存入session域中
globalsession:web项目中,应用在集群环境中, 如果没有集群环境,相当域session

使用

@Component
// 指定对象是单例还是多例,默认是单例
//@Scope("singleton") // 默认值
@Scope("prototype") // 多例
public class Account {}
2.4 生命周期和懒加载

@PostConstruct:用于指定初始化方法,相当于:<bean id="" class="" init-method=""/>

@PreDestroy:用于指定销毁方法:相当于<bean id="" class="" init-method="" destroy-method=""/>

@Lazy:懒加载,只能用于单例对象。

@Component
@Lazy //开启懒加载
public class Account {
    public Account(){
        System.out.println("1. 创建对象");
    }
    // 表示再创建对象之后执行
    @PostConstruct
    public void init_(){
        System.out.println("2. 执行 init 初始化方法");
    }
    // 表示在销毁容器时候执行
    @PreDestroy
    public void destroy_(){
        System.out.println("3. 执行 destroy 释放资源的方法,在关闭容器时候执行。");
    }
}
2.5 配置文件信息注解

​ 将配置文件中的信息也用注解表示,实现纯注解无配置开发。

@Configuration

​ 用于指定当前类是一个 spring 配置类, 当创建容器时会从该类上加载注解。 获取容器时需要使用AnnotationApplicationContext(有@Configuration 注解的类.class)。

@ComponentScan

​ 用于指定 spring 在初始化容器时要扫描的包。 作用和在 spring 的 xml 配置文件中的:<context:component-scan base-package=“com.itheima”/>是一样的。

@PropertySource

​ 用于加载.properties 文件中的配置。例如我们配置数据源时,可以把连接数据库的信息写到 properties 配置文件中,就可以使用此注解指定 properties 配置文件的位置。

@Bean
该注解只能写在方法上,表明使用此方法创建一个对象,并且放入spring 容器

@Import

​ 用于导入其他配置类,在引入其他配置类时,可以不用再写@Configuration 注解。当然,写上也没问题。

3 改造CRUD案例,实现纯注解开发

3.1 配置文件信息注解化
  • xml配置文件类
package config;

//指定当前类为Spring配置类
@Configuration
//指定初始容器时扫描的包
@ComponentScan("com.zmysna")
//导入其他配置文件类
@Import(JdbcConfig.class)
public class SpringConfiguration {
}
  • JDBC注解配置

    1. 配置文件 jdbc.properties
    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8
    jdbc.username=root
    jdbc.password=awsed2zx
    jdbc.initialSize=3
    jdbc.maxActive=50
    
    1. 配置类
    package config;
    
    import com.alibaba.druid.pool.DruidDataSource;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.jdbc.core.JdbcTemplate;
    
    import javax.sql.DataSource;
    
    //加载配置文件
    @PropertySource(value = "classpath:jdbc.properties")
    public class JdbcConfig {
        //@${}读取配置文件信息
        @Value("${jdbc.driverClassName}")
        private String driverClassName;
        @Value("${jdbc.url}")
        private String url;
        @Value("${jdbc.username}")
        private String username;
        @Value("${jdbc.password}")
        private String password;
    
        //使用方法得到的DataSource对象放到容器中,指定名称为dataSource
        @Bean("dataSource")
        public DataSource createDataSource(){
            DruidDataSource druidDataSource = new DruidDataSource();
            druidDataSource.setUsername(username);
            druidDataSource.setPassword(password);
            druidDataSource.setUrl(url);
            druidDataSource.setDriverClassName(driverClassName);
            return druidDataSource;
        }
    
         //使用方法得到的DataSource对象放到容器中,指定名称为jdbcTemplate
        @Bean("jdbcTemplate")
        public JdbcTemplate createJdbcTemplate(@Qualifier("dataSource") DataSource dataSource){
            return new JdbcTemplate(dataSource);
        }
    }
    
3.2 dao层
package com.zmysna.dao.impl;

//创建对象加入到容器中,对象名称是accountDao
@Repository("accountDao") 
public class AccountDaoImpl implements IAccountDao {
    
    //按照类型和名称自动注入jdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
	...各种方法省略
}
3.3 service层
//创建对象加入到容器中,对象名称是accountService
@Service("accountService")
public class AccountServiceImpl implements IAccountService{

    private IAccountDao accountDao;
    
	//按照类型和名称自动注入accountDao(给accountDao赋值)
    @Autowired
    public void setAccountDao(IAccountDao accountDao) {
        this.accountDao = accountDao;
    }
}
3.4 测试
public class AnnoTest {

    @Test
    public void springAnnoTest() {
        //通过AnnotationConfigApplicationContext加载注解配置类,创建容器。
        ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        IAccountService accountService = ac.getBean("accountService", IAccountService.class);
        System.out.println(accountService.getAccountById(1));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值