Spring非注解开发,采用原先的xml方式,在配置一个对象,采用的是xml配置文件的方式,标签使用:<bean></bean>.通过这个注解,向容器中注入该对象。
如:
<bean id="person" class="com.spring.bean.Person" scope="prototype" >
<property name="age" value="19"></property>
<property name="name" value="zhangsan"></property>
</bean>
启动类采用了:ClassPathXmlApplicationContext
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("beans.xml");
Person bean = classPathXmlApplicationContext.getBean(Person.class);
System.out.println(bean.toString());
结果:
Person [name=zhangsan, age=19, nickName=null]
这种方式,所有的配置都在配置文件中,配置多的话,繁琐很不方便,并且容易配置有误。因而。我比较钟爱于注解开发。
@Configuration: 配置类==配置文件,告诉Spring这是一个配置类。
@Bean:给容器中注册一个Bean;类型为返回值的类型,id默认是用方法名作为id,也可以指定beanid的名字。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
String value() default "";
}
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Bean {
//起个名字
@AliasFor("name")
String[] value() default {};
@AliasFor("value")
String[] name() default {};
//
Autowire autowire() default Autowire.NO;
//指定初始化方法
String initMethod() default "";
//指定销毁方法
String destroyMethod() default AbstractBeanDefinition.INFER_METHOD;
}
如:
Bean:
package com.spring.bean;
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) {
super();
this.name = name;
this.age = age;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
/**
* 初始化
*/
public void personInitMethod(){
System.out.println("person-------initMethod");
}
/**
* 销毁
*/
public void personDestroyMethod(){
System.out.println("person-------destroyMethod");
}
}
配置类:
package com.spring.config;
import com.spring.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//配置类==配置文件
@Configuration //告诉Spring这是一个配置类
public class BeanConfig {
//给容器中注册一个Bean;类型为返回值的类型,id默认是用方法名作为id
//initMethod 初始化方法
//destroyMethod 销毁方法
@Bean(name = "Myperson",initMethod = "personInitMethod",destroyMethod = "personDestroyMethod")
public Person person(){
return new Person("zhw", 20);
}
}
测试类:
package com.spring;
import com.spring.bean.Person;
import com.spring.config.BeanConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringAnnotationMain {
public static void main(String[] args) {
//参数是配置类BeanConfig
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
Person bean1 = applicationContext.getBean(Person.class);
System.out.println(bean1);
String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
for (String name : namesForType) {
System.out.println("beanId------"+name);
}
}
}
结果: