spring中3中为bean注入值的办法总结

简介:

有三种办法,分别是:

  • Normal way
  • Shortcut
  • “p” schema

假设我们现在有这么一个bean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public  class  FileNameGenerator
{
     private  String name;
     private  String type;
  
     public  String getName() {
         return  name;
     }
     public  void  setName(String name) {
         this .name = name;
     }
     public  String getType() {
         return  type;
     }
     public  void  setType(String type) {
         this .type = type;
     }
}

  

1. Normal way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
< beans  xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  
     < bean  id="FileNameGenerator" class="com.mkyong.common.FileNameGenerator">
         < property  name="name">
             < value >mkyong</ value >
         </ property >
         < property  name="type">
             < value >txt</ value >
         </ property >
     </ bean >
</ beans >

  

2. Shortcut

1
2
3
4
5
6
7
8
9
10
11
< beans  xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  
     < bean  id="FileNameGenerator" class="com.mkyong.common.FileNameGenerator">
         < property  name="name" value="mkyong" />
         < property  name="type" value="txt" />
     </ bean >
  
</ beans >

  

3. “p” schema

1
2
3
4
5
6
7
8
9
10
< beans  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-2.5.xsd">
  
     < bean  id="FileNameGenerator" class="com.mkyong.common.FileNameGenerator"
              p:name="mkyong" p:type="txt" />
  
</ beans >

  第三种办法需要加入: xmlns:p=”http://www.springframework.org/schema/p

三种办法都很好,具体选哪个,看个人喜好。


==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/12/27/2835094.html,如需转载请自行联系原作者
相关文章
|
1天前
|
Java Spring 容器
解读spring5源码中实例化单例bean的调用链
解读spring5源码中实例化单例bean的调用链
9 1
|
1天前
|
Java Spring
聊聊Spring中两种创建Bean的方式:BeanDefinition.setInstanceSupplier() 和 FactoryBean
聊聊Spring中两种创建Bean的方式:BeanDefinition.setInstanceSupplier() 和 FactoryBean
4 0
|
1天前
|
Java 开发者 Spring
Spring的Bean的生命周期各个阶段扩展方法
Spring的Bean的生命周期各个阶段扩展方法
3 0
|
3天前
|
运维 Java 关系型数据库
Spring运维之boot项目bean属性的绑定读取与校验
Spring运维之boot项目bean属性的绑定读取与校验
12 2
|
8天前
|
Java 开发者 Spring
Spring 中 Bean 的生命周期
Spring 中 Bean 的生命周期
12 2
|
8天前
|
Java 开发者 Spring
解析Spring中Bean的生命周期
解析Spring中Bean的生命周期
15 2
|
8天前
|
XML Java 数据格式
深度解析 Spring 源码:从 BeanDefinition 源码探索 Bean 的本质
深度解析 Spring 源码:从 BeanDefinition 源码探索 Bean 的本质
18 3
|
9天前
|
XML Java 数据格式
Spring框架第三章(基于注解管理bean)
Spring框架第三章(基于注解管理bean)
|
9天前
|
XML Java 数据格式
Spring框架第二章(基于XML管理bean)
Spring框架第二章(基于XML管理bean)
|
12天前
|
SQL 安全 Java
Spring Boot中的跨站点脚本攻击(XSS)与SQL注入防护
【6月更文挑战第15天】在现代Web应用程序开发中,安全性是一个至关重要的课题。跨站点脚本攻击(XSS)和SQL注入是最常见的两种攻击类型,它们可以严重威胁到应用程序的安全。
63 0