您的位置:首页技术文章
文章详情页

浅谈Spring Bean的基本配置

【字号: 日期:2022-08-13 09:37:57浏览:6作者:猪猪
一、Spring中set方法的注入

User实体

@Data//lombok提供的有参构造@AllArgsConstructorlombok提供的无参构造@NoArgsConstructorpublic class User { private int id; private String name; private int age; private String sex; private String birthday;}

beanFactory.xml

<bean class='edu.xalead.User'><property name='id' value='1806'/><property name='name'> <value>张三</value></property><property name='sex' value='男'/><property name='age' value='18'/><property name='birthday' value='2000-1-1'/> </bean>1.1 set的两种注入方法

我们在xml文件中注入的时候我们可以写成这样:

<property name='id' value='1806'/>

也可以写成这样:

<property name='id'> <value>1806</value></property>

这没什么区别的,不过我们一般使用前者,毕竟看起来代码少,也方便

代码测试:

@Test public void test3(){//创建工厂BeanFactory beanFactory = new ClassPathXmlApplicationContext('beanFactory.xml');//从工厂中拿配置好的UserServlet实例User user = beanFactory.getBean(User.class);System.out.println(user); }

浅谈Spring Bean的基本配置

1.2 type属性

有事我们在注入的时候有时候会看到type:

<property name='id'> <value type='int'>1806</value></property>

这相当于一个类型声明,声明value是什么类型的数据,然后调用类型转换器将我们写入的字符串转换为我们我们所定义的类型。但其实这是一个多余的,当我们注入的时候,会通过User对象进行反射从而知道是什么类型。

1.3 Date()类型的注入

但有一些特殊的类型是Spring所没有的,需要我们自己去定义,就比如Date类型,如果我们这样写就会报错

private Date birthday;

<property name='birthday' value='2000-1-1'/>

浅谈Spring Bean的基本配置

Spring是没有这个的转换器,将字符串转换为Date类型,所以其实我们可以直接用String来写,或者new Date(),但是后者约束性太大,不能得到我们想要的日期,还是前者方便。

二、Spring_scop

当我们在测试案例中创建两个User实例时,进行检查发现,这两个实例其实是一个

BeanFactory beanFactory = new ClassPathXmlApplicationContext('beanFactory.xml');//从工厂中拿配置好的UserServlet实例User user = beanFactory.getBean(User.class);User user1 = beanFactory.getBean(User.class);System.out.println(user == user1);

浅谈Spring Bean的基本配置

如果我们要创建多例模式,就要使用到属性scope

scope属性有两个值:

1.singleton(默认情况下,即单例模式)

2.prototype(多例模式)

<bean scope='prototype'>

我们在进行测试答案为false

三、自动注入(autowire)

我们在userServlet中注入userDao

<bean class='edu.xalead.UserDao'></bean><bean class='edu.xalead.UserServlet'> <property name='userDao' ref='userDao'/></bean>

但其实我们没必要这样写,Bean中you自动注入的属性autowire,他有两个值:

1.byName,根据名字注入,即id=“userDao”

<bean class='edu.xalead.UserDao'></bean><bean autowire='byName'/>

2.byType,根据类型注入,类型注入比较有局限性,同种类型只能注入一个,多了会报不是唯一错误

<bean class='edu.xalead.UserDao'></bean><bean autowire='byType'>四、构造注入

<!-- User有个四参构造,我们通过constructor-arg一个一个对应构造参数进行值的注入 --><bean class='edu.xalead.User'> <constructor-arg value='1111'/> <constructor-arg value='zhangsan'/> <constructor-arg value='20'/> <constructor-arg value='M'/></bean>

构造注入和set注入的不同点就是,加入元素的顺序必须和你所创建的实体(User)类相同,若不同,则会报错,由于反射过来的类型和转换器转换的类型不同,这时候我们需要加入另一个属性index

<!-- User有个四参构造,我们通过constructor-arg一个一个对应构造参数进行值的注入 --><bean class='edu.xalead.User'> <constructor-arg value='M' index='3'/> <constructor-arg value='zhangsan' index='1'/> <constructor-arg value='1111'/ index='0'> <constructor-arg value='20' index='2'/></bean>

那什么时候使用构造注入呢?当我们自己定义一个构造函数的时候使用构造注入

public class User { private int id; private String name; private int age; private String sex; private String birthday;//自定义构造函数 public User(int id , String name){this.id = id;this.name = name; }}

<bean class='edu.xalead.User'><constructor-arg value='18'/><constructor-arg value='张三'/> </bean>

这个时候就不能使用set注入,他会报错,即使你写出全参构造函数也不行

浅谈Spring Bean的基本配置

五、Array注入(数组注入)

private String[] photos;

<property name='photos'><array> <value>1.jpg</value> <value>2.jpg</value> <value>3.jpg</value></array></property>六、List注入

private List<String> list;

<property name='list'><list> <value>a</value> <value>b</value> <value>c</value></list></property>七、Set注入

private Set<String> set;

<property name='set'><set> <value>a</value> <value>b</value> <value>c</value></set></property>八、Map注入

private Map<Integer,String> map

<property name='map'><map>//第一种写法 <entry key='1' value='a'/> //第二种写法 <entry key='2'> <value>b</value> </entry> <entry key='3' value='c'/> </map></property>九、Property注入

private Properties prop;

<property name='prop'> <props><prop key='4432'>42341231</prop><prop key='54353'>5464564</prop><prop key='9865'>2659846</prop> </props></property>

到此这篇关于浅谈Spring Bean的基本配置的文章就介绍到这了,更多相关Spring Bean的配置内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!

标签: Spring
相关文章: