一、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 https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 包扫描 只要标注了 @Controller @Service @ Repository @Component 就会被加入到容器中-->
<!-- <context:component-scan base-package="com.hao"></context:component-scan>-->
<bean id="person" class="com.hao.bean.Person">
<property name="age" value="18"></property>
<property name="name" value="张三"></property>
</bean>
</beans>
二、自动注解的方式
package com.hao.config;
import com.hao.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
/**
* @author haoxiansheng
* @date 2020-05-28
*/
@Configuration // 告诉spring 这是一个配置类 // 排除那些
@ComponentScan(value = "com.hao",includeFilters ={@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})}, useDefaultFilters = false)
//useDefaultFilters 禁用默认扫描属性 默认全部扫描
//includeFilters 指定包含那些组件
//excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})} Filter[]: 指定扫描按照什么规则排除那些组件
public class MainConfig {
/**
* 1、给容器注册一个Bean 类型为返回值的类型,id 默认是用方法名作为id
* 2、给Bean 指定名字可以在bean 中指定value的值
*
* @return
*/
@Bean(value = "person")
public Person person01() {
return new Person("李四", 20);
}
}
三、公共代码块
1、Person为例
package com.hao.bean;
/**
* @author haoxiansheng
* @date 2020-05-28
*/
public class Person {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public Person() {
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
2、测试类
package com.hao;
import com.hao.bean.Person;
import com.hao.config.MainConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author haoxiansheng
* @date 2020-05-28
*/
public class MainTest {
public static void main(String[] args) {
// Xml注解测试
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
// Person person = (Person) applicationContext.getBean("person");
// System.out.println(person);
// 自动注解测试
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
Person person=(Person)applicationContext.getBean("person");
System.out.println(person);
String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
for (String name :namesForType) {
System.out.println(name);
}
}
}
四、总结
1、可以看出自动注解省去了大量的配置,比较方便。