• Spring-注解开发


    注解开发定义bean

    1.@Component代替Spring-application.xml核心配置文件中的<bean>标签中的类
    
    • 1

    在这里插入图片描述

    Demo1

    UserDaoImpl.java

    import org.springframework.stereotype.Component;
    
    @Component("userDao")
    public class UserDaoImpl implements UserDao {
        private String username;
        public void setName(String name){
            this.username=name;
        }
        public void save() {
            System.out.println("user dao save ...");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    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
                                "
            >
        <context:component-scan base-package="com.itheima"/>
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    APP4.java

    public class APP4 {
        public static void main(String[] args) {
            ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
            UserDao userDao=(UserDao) ctx.getBean("userDao");
            userDao.save();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    run一下APP4.java
    在这里插入图片描述
    目录结构
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    2.@Controller、@Service、@Repository是@Component的衍生注解 
       @Contorller:用于表现层bean定义
       @Service:用于业务层bean定义
       @Repository:用于数据层bean定义
    
    • 1
    • 2
    • 3
    • 4

    纯注解开发

    在这里插入图片描述
    在这里插入图片描述

    Demo2

    1.注释掉配置文件,这里通过更改文件名的后缀。更改为applicationContext.xml.bak。
    在这里插入图片描述
    2.在com.itheima.config下新建一个SpringConfig文件。
    在这里插入图片描述
    在这里插入图片描述
    SpringConfig.java

    package com.itheima.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.stereotype.Component;
    
    @Configuration
    @ComponentScan("com.itheima")
    public class SpringConfig {
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3.新建APP5进行测试

    APP5.java

    public class APP5 {
        public static void main(String[] args) {
            ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
            UserDao userDao=(UserDao) ctx.getBean("userDao");
            userDao.save();
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    注解开发总结

    在这里插入图片描述

    bean的管理

    @Scope("singleton"):表示创建的bean是单例模式的
    @Scpoe("prototype"): 表示创建的bean不是单例的
    
    @PostConstruct: 构造方法后
    @PreDestroy: 销毁前
    
    • 1
    • 2
    • 3
    • 4
    • 5

    依赖注入

    在这里插入图片描述

    Demo3

    BookDaoImpl.java

    package com.itheima.dao.Impl;
    
    import com.itheima.dao.BookDao;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class BookDaoImpl implements BookDao {
        private int[] array;
        public void setArray(int[] array){
            this.array=array;
        }
        public void save(){
            System.out.println("book dao save .....");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    使用Autowired注解
    BookServiceImpl.java

    @Service
    public class BookServiceImpl implements BookService {
        @Autowired
        private  BookDao bookDao;
    //    private UserDao userDao;
        //提供对应的set方法
        public void setBookDao(BookDao bookDao){
            this.bookDao=bookDao;
        }
        public void save(){
            System.out.println("book service save .....");
            bookDao.save();
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    SpringConfig.java

    @Configuration
    @ComponentScan("com.itheima")
    public class SpringConfig {
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    APP5.java

    public class APP5 {
        public static void main(String[] args) {
            ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
            BookService bookService=ctx.getBean(BookService.class);
            bookService.save();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述
    文件目录结构
    在这里插入图片描述
    注入引用类型
    在这里插入图片描述
    注入简单类型
    在这里插入图片描述
    加载properties文件
    在这里插入图片描述

    @Value("${配置文件的变量名}")
    
    • 1

    在这里插入图片描述

    注解开发管理第三方bean

    在这里插入图片描述

    Demo4

    1.添加druid的依赖

    <dependency>
       <groupId>com.alibaba</groupId>
       <artifactId>druid</artifactId>
       <version>1.1.16</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.配置SpringConfig.java文件

    @Configuration
    //@ComponentScan("com.itheima")
    public class SpringConfig {
    //    1.定义方法获得要管理的对象
    //    2.添加@Bean,表示当前返回值是一个Bean
        @Bean("dataSource")
        public DataSource dataSource(){
            DruidDataSource ds=new DruidDataSource();
            ds.setDriverClassName("com.mysql.jdbc.Driver");
            ds.setUrl("jdbc:mysql://localhost:3306/spring_db");
            ds.setUsername("root");
            ds.setPassword("root");
            return ds;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3.测试

    public class APP6 {
        public static void main(String[] args) {
            AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
            DataSource dataSource=ctx.getBean(DataSource.class);
            System.out.println(dataSource);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述
    文件目录结构
    在这里插入图片描述

    Demo5

    demo5在demo4的基础上进一步解耦,将原先存在在SpringConfig中jdbc的相关内容分到jdbcConfig中。
    1.定义jdbcConfig文件
    在这里插入图片描述

    public class jdbcConfig {
        @Bean
        public DataSource dataSource(){
            DruidDataSource ds=new DruidDataSource();
            ds.setDriverClassName("com.mysql.jdbc.Driver");
            ds.setUrl("jdbc:mysql://localhost:3306/spring_db");
            ds.setUsername("root");
            ds.setPassword("root");
            return ds;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.更改SpringConfig文件

    @Configuration
    @Import({jdbcConfig.class})
    //@ComponentScan("com.itheima")
    public class SpringConfig {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3.运行APP6.java查看结构

    public class APP6 {
        public static void main(String[] args) {
            AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
            DataSource dataSource=ctx.getBean(DataSource.class);
            System.out.println(dataSource);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述
    文件结构目录
    在这里插入图片描述

    第三方bean依赖注入

    在这里插入图片描述
    在这里插入图片描述

    注解开发总结

    在这里插入图片描述

  • 相关阅读:
    【HackTheBox】dancing(SMB)
    二进制信号量
    Batbot智慧能源管理云平台:拥抱数字化,提高能源效率!
    读 | SA : The Hard Parts 之数据访问
    QT 小知识随记
    Node.js 的 CommonJS & ECMAScript 标准用法
    基于python的电影爬虫可视化系统设计与实现
    MATLAB中print函数使用
    在线兴趣教学类线上学习APP应用开发部署程序组建研发团队需要准备什么?
    安卓手持机 条码扫描终端 物流仓储盘点机
  • 原文地址:https://blog.csdn.net/weixin_42888638/article/details/125474273