spring中的ico:(Inverse Object Control)

一,要用spring的步骤:


1,至少要导入的jar包:

spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.log4j-1.2.15.jar

2,创建applicationContext.xml配置文件(告诉bean工厂(spring就相当于一个bean工厂)生成哪些对象)

3,   通过bean工厂来获取对象


二,di(Dependency Injection依赖注入):

概念:通过ioc创建的bean的时候,给bean的属性赋值的操作就叫依赖注入


三,注入的方法

bean的属性注入方式有3种
1,set
方法注入

就是在实体类中生成的set/get方法来操作的(applicationContext.xml的内容,下面的配置文件中有代码


2,构造方法注入


3,命名空间注入

四,获取bean工厂的方法

applicationContext.xml内容

<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">
<!-- 构造方法注入属性值
Users users2 = new Users(1,"三狗子");
-->

<bean id="users2" class="com.kz.entity.Users">
<!--1,与参数的类型顺序有关
<constructor-arg value="1"></constructor-arg>
<constructor-arg value="三狗子"></constructor-arg>
-->
<constructor-arg index="1" type="java.lang.String" value="三狗子"></constructor-arg>
<constructor-arg index="0" type="int" value="1"></constructor-arg>

</bean>
<bean id="users" class="com.kz.entity.Users">
<!-- 通过set方法来给属性注入值 -->
<property name="name" value="二狗子"></property>
<property name="age" value="2"></property>
<property name="glag" value="true"></property>
<!-- 数组 -->
<property name="hobbies">
<list>
<value>tom</value>
<value>jack</value>
<value>kate</value>
</list>
</property>
<!-- list -->
<property name="list">
<list>
<value>tom2</value>
<value>jack2</value>
<value>kate2</value>
</list>
</property>
<!-- set -->
<property name="set">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<!-- map -->
<property name="map">
<map>
<entry key="1" value="长沙"></entry>
<entry key="2" value="株洲"></entry>
</map>
</property>
<!-- list:对象 -->
<property name="stus">
<list>
<ref bean="student"></ref>


</list>
</property>
</bean>
<!-- student -->
<bean id="student" class="com.kz.entity.Student">
<property name="name" value="王麻子"></property>
</bean>
<!-- 包含多个配置文件
,引用其它的配置文件
-->
<import resource="beans.xml"/>

<!-- 命名空间p:注入属性 -->
<bean id="users3" class="com.kz.entity.Users" 
p:id="1" p:name="四狗子"></bean>
</beans>


三种:    1,     beanFactory

Resource resource = (Resource) new ClassPathResource("applicationContext.xml");
//到对应的文件存放的磁盘中查找
//Resource resource = new FileSystemResource("src\\applicationContext.xml");
BeanFactory bFactory = new XmlBeanFactory(resource);
Users user = (Users) bFactory.getBean("users3");
System.out.println(user);


2, bean工厂上下文对象ApplicationContext

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Users user =(Users) context.getBean("users3");
System.out.println(user);



3, web开发:WebApplicationContext(继承struts2由web容器(tomcat)创建)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值