spring高级功能

自动装配

Spring能自动装配Bean与Bean之间的依赖关系,无需使用ref显示指定依赖Bean。
通过元素的default-autowire属性指定,也可以通过autowire属性指定。
no:不使用自动装配,必须通过ref定义。
byName:根据属性名自动装配
byType:根据属性类型自动装配
constructor:根据属性名自动装配,用构造函数
autodetect:由beanFactory决定用byType或constructor,有默认构造函数,用byType;

Spring启动时将自动搜索,自动装配。若不想自动装配,可使用
autowire-candidate="false"

例1:

Reply.java

package auto;

public class Reply {
	private String id;
	private String title;
	private String content;
	private Topic topic;

	// 实现构造函数

	public Reply() {
		System.out.println("reply被创建");
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Topic getTopic() {
		return topic;
	}

	public void setTopic(Topic topic) {
		this.topic = topic;
	}

}

Topic.java

package auto;

import java.util.List;

public class Topic {
	private String id;
	private String title;
	private String Content;

	// 集合继承
	private List names;

	public List getNames() {
		return names;
	}

	public void setNames(List names) {
		this.names = names;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getContent() {
		return Content;
	}

	public void setContent(String content) {
		Content = content;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

}

applicationContext.xml




	
		
			1
		
		
			xxxx
		
		
			yy
		
	
	
	  
	
		
		 
			1
		
		
			zhenhao
		
		
			xuexi
		
		
	 
测试代码

                 /*
		 * 根据XML文件查询
		 */
			  ApplicationContext context=new  ClassPathXmlApplicationContext("applicationContext.xml"); 
			  Reply  reply=context.getBean("reply",Reply.class);
			  System.out.println(reply.getTopic().getTitle());
			 

运行结果:

reply被创建
xxxx


通过配置类查询,构造一个注释配置类

Config.java

package cofig;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import auto.Topic;

//注解注入,将config变为配置类
@Configuration 
public class Config {
	//给属性赋值
    @Value(value="csdn")   
	public String name;
    
    //得到对象,生成对象名为topic3
    @Bean(name="topic3")
    public Topic getTopic(){
    	Topic topic =new Topic();
    	topic.setTitle("java");
    	topic.setContent("ccxcc");
    	return topic;
    }
    
       
}

测试代码

                /*
		 * 通过配置类查询,构造一个注释配置类,可以进行人工干预
		 */
		
		  ApplicationContext context=new
		  AnnotationConfigApplicationContext(Config.class); Topic
		  topic=context.getBean("topic3",Topic.class);
		  
		  System.out.println(topic.getTitle()+"--"+topic.getContent());

运行结果:

java--ccxcc


继承父类标签

在XML文件中配置


	
	   
	
	   
	      789
	   
	
	

测试代码

		/**
		 * 继承集合类,输出集合中的所有属性
		 */
		
		  ApplicationContext context=new
		  ClassPathXmlApplicationContext("applicationContext.xml"); Topic
		  topic=context.getBean("topic1",Topic.class);
		  
		  System.out.println(topic.getNames());

运行结果:

reply被创建
[123, 245, 789]


不同类型的继承

Talk.java

package auto;

import java.util.List;

public class Talk {
	private String id;
	private String title;
	private String Content;
	private List names;

	public String getId() {
		return id;
	}

	// 实现构造函数

	public Talk() {

		System.out.println("talk被创建");
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return Content;
	}

	public void setContent(String content) {
		Content = content;
	}

	public List getNames() {
		return names;
	}

	public void setNames(List names) {
		this.names = names;
	}

}

在XML文件中配置


	
	
	 

测试代码

	/**
		 * 不同类型的继承
		 */		
		 ApplicationContext context=new
		 ClassPathXmlApplicationContext("applicationContext.xml"); Talk
		 topic=context.getBean("talk",Talk.class);
		 
		 System.out.println(topic.getNames());
		 System.out.println(topic.getTitle());

运行结果:

talk被创建
reply被创建
[123, 245]
xxxx


工厂TopicFactory.java

package factory;

import org.springframework.beans.factory.FactoryBean;

import auto.Topic;

public class TopicFactory  implements  FactoryBean{
	
	 private Topic topic;
	//返回对象
	@Override
	public Topic getObject() throws Exception {
		//单例模式
		if(topic==null){
			topic=new Topic();
		}
			return topic;
		
	}
    //返回对象类型
	@Override
	public Class getObjectType() {
		
		return Topic.class;
	}

	//判断对象是否是单例
	@Override
	public boolean isSingleton() {
		
		return true;
	}

}

bean.xml



	
	
 
  

 
  测试代码: 
  

// 工厂		
		  ApplicationContext context = new ClassPathXmlApplicationContext(
		  "bean.xml"); Topic topic = context.getBean("topic", Topic.class);
		  topic.setId("110"); System.out.println(topic.getId()); Topic topic1 =
		  context.getBean("topic", Topic.class);
		  System.out.println(topic1.getId());
		 

运行结果:

110
110





你可能感兴趣的:(spring)