Spring【使用注解开发、配置类】

使用注解开发

环境准备

使用注解需要导入 context 约束,增加注解支持!

标准的配置文件是applicationContext.xml,当然也可以随意自定义。




    
    

    

1、Bean

//等价于 
//@Component 组件
@Component  //说明User类被管理了
public class User {
//...
}

2、属性如何注入

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

//等价于 
//@Component 组件
@Component  //说明User类被管理了
public class User {
    //相当于 
    @Value("李元芳")
    public String name;

    //两种方法都一样 优先调用这种
    @Value("李元芳")
    public void setName(String name) {
        this.name = name;
    }
}

3、衍生的注解

@Component有多个衍生注解:我们在web开发中,会按照mvc三层架构分层!

  • dao【@Respository】
  • service【@Service】
  • controller【@Controller】

多个包的话,需要在我们的配置文件中指定上层包名:




    
    

    

4、自动装配置

上一节已经写过了

  • @Autowried

  • @Qualifier(搭配@Autowried使用)

  • @Resource

5、作用域

@Component  
@Scope("singleton")    //标注为单例模式
public class User {
//...
}

6、小结

xml 与 注解:

  • xml适用于任何场合,维护简单方便
  • 注解不能引用别的类(Bean),维护相对复杂!

xml 与 注解的最佳实现:

  • xml用来管理Bean
  • 注解只负责属性的注入 

完全使用Java的方式配置Spring

使用一个Java配置类可以实现完全不依赖配置文件 applicationContext.xml 。

JavaConfig 是Spring的一个子项目,在Sring4之后成为了核心功能。

实体类

package com.study.pojo;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

//这个注解的意思是 这个类被Spring接管了 注册到了容器中
@Component
public class User {
    private String name;

    public String getName() {
        return name;
    }

    @Value("李元芳")   //注入名字
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}

配置类

package com.study.config;

import com.study.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

//这个也会被注册到Spring容器中 因为它本身也是一个Component
//@Configuration 是一个配置类 相当于一个 beans.xml
@Configuration
@ComponentScan("com.study.pojo")
@Import(MyConfig.class) //导入另一个配置类
public class MyConfig {

    /**
     *  注册一个bean,相当于
     *     
     */
    @Bean
    public User getUser(){
        return new User();
    }

}
package com.study.config;

import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig2 {


}

测试类

import com.study.config.MyConfig;
import com.study.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Test {

    public static void main(String[] args) {
        //如果完全使用了配置类的方式,我们就只能通过 AnnotationConfig 上下文来获取容器,通过配置类的 class 来加载
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        User getUser = (User) context.getBean("getUser");   //对应方法名
        System.out.println(getUser.getName());
    }
}

这种纯Java的配置方式在SpringBoot中随处可见!

IOC: 将对象的创建和依赖关系管理的责任从应用程序代码中转移到框架或容器中。

你可能感兴趣的:(Spring,spring,java,后端)