java配置

1、Java配置类装配bean
既然是通过Java代码来装配bean,是需要我们自己通过配置类来声明我们的bean。我们先通过@Configuration注解来创建一个Spring的配置类,该类中包含了bean的创建细节——
1.1、 导入jar包或引用maven:
spring-aop-5.0.14.RELEASE.jar 点这里
如果是maven工程,代码如下:

org.springframework
spring-test
5.3.2
test

1.2、 写一个生成Bean的类文件(类似于xml文件的class文件)
/**

  • @author Nical
    */
    @Configuration
    //指定该类为spring ioc容器配置类,相当于beans.xml文件
    @ComponentScan(basePackages=“com.cc.factory2”)
    //开启扫描,会去com.cc.config扫描
    //@Component @Value @Autowired进行创建bean或注入属性值
    public class BeansConfig {
    @Bean(name=“redPig”)
    //将方法返回值纳入到spring ioc容器进行管理,相当于
    public Pig getRedPig() {
    RedPig redPig = new RedPig();
    redPig.setAge(12);
    redPig.setName(“王二麻子”);
    return redPig;
    }
    }

1.3、 测试类
/**

  • @author Nical
    */
    public class TestConfig {
    public static void main(String[] args) {
    AnnotationConfigApplicationContext context=
    new AnnotationConfigApplicationContext
    (BeansConfig.class);
    //通过Java配置类告诉spring bean工厂生产什么样的bean
    RedPig redPig = (RedPig) context.getBean(RedPig.class);
    //@Bean 用于匹配没有设置name属性的@Bean方法
    System.out.println(“redPig==>”+redPig);
    RedPig redPig2 = (RedPig) context.getBean(“redPig”);
    //@Bean(name=“redPig”) 用于匹配设置了name属性的@Bean方法
    System.out.println(“redPig2==>”+redPig2);
    }
    }

2、通过注解配置文件装配bean
2.1、 xml+注解配置 (在xml中开启注解配置):
注意:这里需要额外导入spring-aop的jar包 点这里
在xml中加入如下配置:

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

context:annotation-config/

<context:component-scan base-package=“entity”/>

2.2、 实体类:
/**

  • @author Nical
    */
    @Component
    public class User {
    @Value(“张三”)
    //默认会调用该类的set方法,如果没有则会报错
    private String name;
    @Value(“19”)
    private int age;
    @Autowired
    //按类型来注入,所以扫描的类中必须要有该类型
    private Dog dog;
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public Dog getDog() {
    return dog;
    }
    public void setDog(Dog dog) {
    this.dog = dog;
    }

2.3、 测试类:
/**

  1. @author Nical
    */
    public class TestAnnotaion {
    public static void main(String[] args) {
    ClassPathXmlApplicationContext context =
    new ClassPathXmlApplicationContext
    (“spring-beans-annotation.xml”);
    User user = (User) context.getBean(“user”);
    System.out.println(“user===>” + user);
    }
    }
  1. @Component 被标示类会被纳入spring ioc容器进行管理,相当于
  2. < bean>
  3. @Value 为spring中所有管理的该类对象注入基本类型和String属性值
  4. @Autowired 为spring中所有管理的该类对象注入引用类型属性值;默认按类型注入,可以通过@Qualifier(“dog2”)指定注入哪个bean,同时也可以通过bean加入primary=“true” 优先被Autowired注入
  5. @Resource 默认按类型注入,如果指定了name属性,则按bean名称注入
    这两种方式没有什么所谓的优劣之分,主要看使用情况,一般来说是这样:
  • 涉及到全局配置的,例如数据库相关配置、MVC相关配置等,就用JAVA配置的方式
  • 涉及到业务配置的,就使用注解方式
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值