首页 > 其他分享 >spring之不要xml配置!

spring之不要xml配置!

时间:2025-02-07 11:00:43浏览次数:2  
标签:xml spring 配置 dog cat import Dog Cat public

@Configuration注解

在java类前面加上@Configuration,该类就等价于ApplicationContext.xml(IOC容器的xml配置)了

如下代码就是一个@Configuration注解标注的类

package com.loubin.config;

import com.loubin.pojo.Cat;
import com.loubin.pojo.Dog;
import com.loubin.pojo.People;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.loubin.pojo")
public class Myconfig {
    @Bean
    public People people(){
        return new People();
    }

    @Bean Cat cat(){
        return new Cat();
    }

    @Bean Dog dog(){
        return new Dog();
    }
}

 

该类等价于下面的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.loubin.pojo"/>

    <bean class="com.loubin.pojo.People" id="people"/>
    <bean class="com.loubin.pojo.Cat" id="cat"/>
    <bean class="com.loubin.pojo.Dog" id="dog"></bean>
</beans>

 

@Autowired注解

上一小节虽然使用configuration注解定义类bean,但是People类中有属性为Cat和Dog的对象,而configuration类中并没有显示声明如何对这两个属性进行注入,所以需要搭配上@Autowired注解,该注解现根据类型寻找bean进行注入,如果找不到,再根据name寻找,如下代码所示

package com.loubin.pojo;

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


public class People {
    @Autowired
    private Dog dog;
    @Autowired
    private Cat cat;

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    @Override
    public String toString() {
        return "People{" +
                "dog=" + dog +
                ", cat=" + cat +
                '}';
    }
}

 

@component,@repository, @service,@Controller

还可以进一步精简上两节的代码,即我们不需要再configuration类中显示的定义bean,只需要在相关的类上加入@component,@Controller,@repository或者 @service即可,加上之后就相当于定义了bean。

按照约定,一般在实体类上加@component注解,在持久化层的dao类上加@repository注解,在service层的类上加上@Service,在controller层的类上加上@Controller。

以上的四个注解搭配@Qualifier便可以定义bean的名字,如下代码所示

package com.loubin.pojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

@Component
@Qualifier("people")
public class People {
    @Autowired
    private Dog dog;
    @Autowired
    private Cat cat;

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    @Override
    public String toString() {
        return "People{" +
                "dog=" + dog +
                ", cat=" + cat +
                '}';
    }
}

 

标签:xml,spring,配置,dog,cat,import,Dog,Cat,public
From: https://www.cnblogs.com/loubin/p/18702211

相关文章