spring高级功能
2014-03-23 18:44
232 查看
自动装配
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
Topic.java
applicationContext.xml
运行结果:
reply被创建
xxxx
通过配置类查询,构造一个注释配置类
Config.java
测试代码
运行结果:
java--ccxcc
继承父类标签
在XML文件中配置
测试代码
运行结果:
reply被创建
[123, 245, 789]
不同类型的继承
Talk.java
在XML文件中配置
测试代码
运行结果:
talk被创建
reply被创建
[123, 245]
xxxx
工厂TopicFactory.java
bean.xml
测试代码:
运行结果:
110
110
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
相关文章推荐
- 带高级搜索、书签功能、文件菜单等功能的Hibernate 3.2 Spring 2.0的chm格式Reference
- springmvc+mybatis+sql server实现简单登录功能
- Screenshot Captor绿色版-功能繁多的高级截图软件
- Spring 事务管理高级应用难点剖析
- Spring高级事务管理难点剖析
- git 高级功能
- Spring+MongoDB实现登录注册功能
- InstallShield 2012 Spring新功能试用(8): Suite/Advanced UI 和 Advanced UI工程支持自动检测升级和下载
- spring+springmvc+mybatis+mysql实现登录功能(下)
- SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释
- [置顶] mybatis高级应用系列一:分页功能
- Mybatis学习笔记(3)之高级功能
- 动态代理手动实现Spring AOP的基本功能
- Django在OS X下的编程实战(三)-构建博客的高级功能
- 菜鸟级springmvc+spring+mybatis整合开发用户登录功能(上)
- 关于Spring Aop,日志功能简单的实现
- springMvc+webUploader图片从后台取出,回显后,添加图片删除功能
- 在Spring Boot框架下使用WebSocket实现聊天功能
- Spring高级程序设计这本书怎么样
- SpringBoot整合Mybatis实现增删改查的功能