标签:02 容器 www -- Spring 创建对象 springframework bean 注入
IOC创建对象方式
1.调用无参数构造器
<bean id="user" class="cn.yan.spring.User"></bean>
2.调用有参构造函数
①通过下标
<bean id="user" class="com.peach.pojo.User">
<constructor-arg index="0" value="peach"></constructor-arg>
</bean>
②通过类型赋值(如果两个参数都是String类型,则没有办法使用,所以不建议使用第二种)
<bean id="user" class="com.peach.pojo.User">
<constructor-arg type="java.lang.String" value="peach"/>
</bean>
③直接通过参数名实现
<bean id="user" class="com.peach.pojo.User">
<constructor-arg name="name" value="peach"></constructor-arg>
</bean>
注意:
在配置文件中,容器中的bean 就已经创建出来了。
Spring 配置
1.别名
2.bean 的配置
3.import
一般用于团队开发使用,导入合并为一个配置内容
依赖注入( Dependency Injection)
解释:
- bean对象的创建依赖于容器
- bean 对象中所有的属性,都由容器来注入·。
1.构造器注入
ioc创建对象方式有参构造器注入一样
2.set 方式注入
①普通值注入
②bean注入
③数组注入
④List 注入·
⑤Map注入
⑥Set注入
⑦ null值注入
⑧Properties 注入
3.其他方式注入
p命名空间注入
xmlns:c="http://www.springframework.org/schema/p"
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="classic" class="com.example.ExampleBean">
<property name="email" value="someone@somewhere.com"/>
</bean>
<bean name="p-namespace" class="com.example.ExampleBean"
p:email="someone@somewhere.com"/>
</beans>
c命名空间导入
xmlns:c="http://www.springframework.org/schema/c"
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="beanTwo" class="x.y.ThingTwo"/>
<bean id="beanThree" class="x.y.ThingThree"/>
<!-- traditional declaration with optional argument names -->
<bean id="beanOne" class="x.y.ThingOne">
<constructor-arg name="thingTwo" ref="beanTwo"/>
<constructor-arg name="thingThree" ref="beanThree"/>
<constructor-arg name="email" value="something@somewhere.com"/>
</bean>
<!-- c-namespace declaration with argument names -->
<bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
c:thingThree-ref="beanThree" c:email="something@somewhere.com"/>
</beans>
标签:02,容器,www,--,Spring,创建对象,springframework,bean,注入 来源: https://blog.csdn.net/tianynnb/article/details/118703342
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。