spring高级功能

自动装配

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

Spring启动时将自动搜索,自动装配。若不想自动装配,可使用
autowire-candidate="false"
<beans default-autowire-candidates="*user"(4.0不起作用*)

例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<String> names;

	public List<String> getNames() {
		return names;
	}

	public void setNames(List<String> 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

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire-candidates="*pic"
	xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

	<bean name="topic" class="auto.Topic">
		<property name="id">
			<value>1</value>
		</property>
		<property name="title">
			<value>xxxx</value>
		</property>
		<property name="content">
			<value>yy</value>
		</property>
	</bean>
	<!-- autowire注入,通过byName找name注入,当spring初始化,id跟name一样效果(name可以放多个,id只能放一个 ),不能有同名。autowire-candidate="false"不让它自动装配(4.0版本byName不能出现结果) -->
	 <bean name="reply" class="auto.Reply"   autowire="byName"> 
	<!-- <bean id="reply" class="auto.Reply" autowire="byName">-->
		<!-- 根据类型注入,如果有多个对象是抛出异常 ,两个bean会出错 ,根据byType查询时 autowire-candidate="false"起作用 
			<bean id="reply" class="auto.Reply" autowire="byType" > -->
		 <property name="id">
			<value>1</value>
		</property>
		<property name="title">
			<value>zhenhao</value>
		</property>
		<property name="content">
			<value>xuexi</value>
		</property>
		<!-- 同时注入和ref同时使用,以ref为主 -->
	</bean> 
</beans>
测试代码

                 /*
		 * 根据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文件中配置

<!--  默认找topic的类,继承父标签的值 -->
	<bean  name="topic1"  parent="topic">
	   <property name="names">
	<!--    merge="true"回合上面的,合并一起,合并集合的值 -->
	   <list  merge="true">
	      <value>789</value>
	   </list></property>
	</bean>
	

测试代码

		/**
		 * 继承集合类,输出集合中的所有属性
		 */
		
		  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<String> 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<String> getNames() {
		return names;
	}

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

}

在XML文件中配置

<!-- 不同类型的继承 -->
	<bean  name="talk"  class="auto.Talk"  parent="topic">
	
	</bean> 

测试代码

	/**
		 * 不同类型的继承
		 */		
		 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<Topic>{
	
	 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

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire-candidates="*pic"
	xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
	
	<!-- 测试depend-on 
	不加depends-on="reply"
	根据顺序有关。如果加就跟顺序无关;lazy-init="true"懒加载等待完成后在运行<bean  name="        reply" class="auto.Reply"  lazy-init="true"></bean>
        <bean  name="talk" class="auto.Talk"  depends-on="reply"></bean> 
        -->
<!-- 证明工厂是否是单例 ,配置单例工厂--> <bean name="topic" class="factory.TopicFactory"> </bean> </beans>


测试代码:

// 工厂		
		  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高级功能,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。