肝了这篇入门Spring框架(注解)超详细

肝了这篇入门Spring框架(注解)超详细_第1张图片

 

养成阅读好习惯,从关注开始!

1. 常用注解

xml配置spring


    



1.1 用于创建对象的注解

此类注解的作用就和在XML配置文件中编写一个标签实现的功能是一样的,

相当于xml配置中的

@Component 用于把当前类对象存入spring容器中

属性:value:用于指定bean的id。当我们不写时,它的默认值是当前类名,且首字母小写。

@Controller 一般用在表现层

@Service 一般用在业务层

@Repository 一般用在持久层

以上三个注解他们的作用和属性与Component是一模一样,可以不按照规定写注解。他们三个是spring框架为我们提供明确的三层使用的注解,使我们的三层对象更加清晰。

肝了这篇入门Spring框架(注解)超详细

 

1.2 用于注入数据的注解

此类注解的作用就和在xml配置文件中的bean标签中写一个标签的作用是一样的。

相当于xml配置中的

@Autowired 自动按照类型注入。

  • 如果容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功。
  • 如果ioc容器中没有任何bean的类型和要注入的变量类型匹配,则报错。
  • 如果Ioc容器中有多个类型匹配时:Autowired在进行匹配时会进行类型匹配,如果有唯一的类型匹配,则进行直接注入。如果IoC中有多个类型匹配,会首先按照类型确定匹配对象,接下来使用变量名称作为Bean的id在确定的范围内继续查找,如果找到则注入成功,如果找不到则报错。欢迎大家进Java学习交流君样:1142951706 一起交流 一起吹水~

注解位置:可以是变量上,也可以是方法上。

肝了这篇入门Spring框架(注解)超详细

 

@Qualifier 在按照类中注入的基础之上再按照名称注入,需要和@Autowired注解配合使用。它在给类成员注入时不能单独使用。但是在给方法参数注入时可以。

属性:value:用于指定注入bean的id。

@Resource 直接按照bean的id注入。它可以独立使用,通过名称Bean名称进行依赖注入。 属性:name:用于指定bean的id。

肝了这篇入门Spring框架(注解)超详细_第2张图片

 

@Value 用于注入基本类型和String类型的数据

属性:value:用于指定数据的值。它可以使用spring中SpEL(也就是spring的el表达式)

SpEL的写法:${表达式}

1.3 用于修改作用范围的注解

此注解的作用就和在bean标签中使用scope属性实现的功能是一样的

相当于xml配置中的

@Scope

作用:用于指定bean的作用范围

属性:value:指定范围的取值。常用取值:singleton 单例的 prototype 多例的

1.4 与声明周期相关的注解

此类注解的作用就和在bean标签中使用init-method和destroy-methode的作用是一样的

相当于xml配置中的

@PostConstruct 用于指定初始化方法

@PreDestroy 用于指定销毁方法

代码示例:

@Service
//@Scope("prototype")
public class AccountServiceImpl implements AccountService {
    //    @Autowired
    //    @Qualifier("accountDao1")
    @Resource(name = "accountDao2")
    private AccountDao accountDao;
    @Value(value = "Bruce")
    private String name;
    @Value("30")
    private Integer age;
    @Autowired
    private Date birthday;
    @PostConstruct
    public void init() {
        System.out.println("初始化方法执行。。。。");
    }
    @PreDestroy
    public void destory() {
        System.out.println("销毁方法执行。。。。");
    }
    public void saveAccount() {
        System.out.println("Service创建的账户保存了");
        accountDao.saveAccount();
    }
    public void show() {
        System.out.println(name + " " + age + " " + birthday);
    }
}

 

肝了这篇入门Spring框架(注解)超详细

 

1.5 spring中xml配置和注解之间的关系

注解配置优势:配置简单,易于维护(配置都在对应的类上)

xml配置的优势:修改时不需要重新编译,通过外部引入

spring中Bean管理方式的对比

肝了这篇入门Spring框架(注解)超详细_第3张图片

 

肝了这篇入门Spring框架(注解)超详细

 

肝了这篇入门Spring框架(注解)超详细_第4张图片

 

肝了这篇入门Spring框架(注解)超详细_第5张图片

 

通过注解获取IoC容器:

AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);

3. 注解配置和XML配置对比

3.1 xml配置

书写在src下的resources中


    
    
        
        
        
        
        
    
    
    
        
        
    
    
    
        
        
    
    
    
        
        
    

3.2 注解配置配置

配置代码书写在config包中与项目包平级

SpringConfiguration类

@ComponentScan("cn.bruce")
@Import(JdbcConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {
}

JdbcConfig类

public class JdbcConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    /**
     * 创建数据源对象
     * @return
     */
    @Bean(name = "ds")
    public DataSource creatDataSource(){
        ComboPooledDataSource ds = new ComboPooledDataSource();
        try {
            ds.setDriverClass(driver);
            ds.setJdbcUrl(url);
            ds.setUser(username);
            ds.setPassword(password);
            return ds;
        } catch (PropertyVetoException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 创建QueryRunner对象
     * @param dataSource
     * @return
     */
    @Bean(name = "runner")
    @Scope("prototype")
    public QueryRunner creatQueryRunner(@Qualifier("ds") DataSource dataSource){
        return new QueryRunner(dataSource);
    }
}

 

肝了这篇入门Spring框架(注解)超详细

 

3.3. spring整合Junit单元测试

Junit执行问题:

  1. 应用程序的入口:main方法
  2. Junit中单元测试方法,没有main方法,此方法会判断当前测试类中那些方法有@Test注解,Junit会选择有@Test注解的方法执行
  3. Junit在执行测试方法时,Junit不知道是否使用Spring框架,因此不会读取配置文件/配置类也不会创建IoC容器
  4. 因此当测试方法执行时没有创建容器,会产生空指针异常,欢迎大家进Java学习交流君样:1142951706 一起交流 一起吹水~

Spring整合Junit:

  1. 导入spring整合junit的jar(坐标)
  2. 使用Junit提供的一个注解把原有的main方法替换了,替换成spring提供的@Runwith
  3. 告知spring的运行器,spring和ioc创建是基于xml还是注解的,并且指明位置 @ContextConfiguration locations:指定xml文件的位置,加上classpath关键字,表示在类路径下 classes:指定注解类所在地位置

代码示例

maven坐标:


    org.springframework
    spring-test
    5.2.8.RELEASE

test测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {
    @Autowired
    private AccountService accountService = null;
    @Test
    public void testFindAll(){
        List accounts = accountService.findAll();
        for(Account account : accounts){
            System.out.println(account);
        }
    }
}

注意:

① 当我们使用spring 5.x版本的时候,要求junit的jar必须是4.12及以上

② 当通过xml配置spring时,本地路径注意不要有空格@ContextConfiguration(locations = "classpath:beans.xml")

最后说一句,如果本文对你有帮助,请三连支持一下哦!

你可能感兴趣的:(后端,编程语言,程序人生,数据库,spring,java,mysql,spring,boot)