【SpringBoot框架01】spring5.x应用零配置开发

【SpringBoot框架01】spring5.x应用零配置开发

思维导图

在这里插入图片描述

一、IOC中Bean的实例化与获取

1.配置pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.lcy</groupId>
  <artifactId>spring08-AOP</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>spring08-AOP</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.1.5.RELEASE</version>
    </dependency>
  </dependencies>

  <build>
<!--    指定maven编译的jdk版本-->
    <plugins>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.3.2</version>
          <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>utf-8</encoding>
          </configuration>
        </plugin>
      </plugins>
  </build>
</project>

2.创建Bean对象

package com.lcy.springboot.dao;

public class UserDao {
    public void test(){
        System.out.println("success!!!!!!!!!!!!!!!!!!!!!!!");
    }
}

package com.lcy.springboot.service;

@Service
public class UserService {
    @Autowired
    public UserDao userDao;
    public void test01(){
        System.out.println("userService successful!!!!!!!!!!");
        userDao.test();
    }
}

3.创建配置类

package com.lcy.springboot.Config;

//声明当前类是一个配置类
@Configuration
//设置包扫描范围
@ComponentScan("com.lcy.springboot")
public class IocConfig {
}

4.创建启动类执行测试

package com.lcy.springboot;

public class TestMyDome {
    public static void main(String[] args) {
//        基于java配置类加载spring的应用上下文
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext( IocConfig.class);
//        获取指定的Bean对象
        UserService userService = applicationContext.getBean(UserService.class);
//        调用Bean对象方法
        userService.test01();
    }
}

二、@Bean注解(作用在方法上)使用

通常用于整合第三方的bean对象【数据源、第三方对象(单一数据源)】

1.创建Bean对象,不加注解

package com.lcy.springboot.dao;

public class AccountDao {
    public void test(){
        System.out.println("AccountDao Success!!!!!!!!!!!!!!!!!!!");
    }
}

2.修改IocConfig配置类

添加@Bean注释
package com.lcy.springboot.Config;

@Configuration
@ComponentScan("com.lcy.springboot")
public class IocConfig02 {
    @Bean
    public AccountDao accountDao(){
        return new AccountDao();
    }
}

3.创建启动类并测试

package com.lcy.springboot;

public class TestMyDome {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(IocConfig02.class);
        IocConfig02 iocConfig02 = applicationContext.getBean(IocConfig02.class);
//        是一个单例对象
        System.out.println(applicationContext.isSingleton("iocConfig02"));
        iocConfig02.accountDao().test();
    }
}

三、读取外部配置文件

1.准备配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mygame?useUnicode=true&characterEncoding=utf8
jdbc.user=root
jdbc.password=root

2.@PropertySource加载配置文件

@Configuration
@ComponentScan("com.lcy.springboot")
@PropertySource(value={"classpath:jdbc.properties")
public class IocConfig02 {
    @Value("${jdbc.driver}")
    private String driver;
   public void test(){
        System.out.println(driver);
    }
}

四、组合注解和元注解(注解在注解上的注解)

1.自定义组合注解

要求:拥有@Configuration和@ComponentScan两者功能

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Configuration
@ComponentScan
public @interface MyAnnotation {
    String[] value() default {};
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值