- 各种数据类型的注入
-
<?xml version="1.0" encoding="UTF-8"?> <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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!-- set方法注入(1.必须要有无参的构造方法,2.必须要有set方法 --> <bean id="s1" class="com.pojo.Student"> <property name="stuName" value="陈帅"></property> <property name="age" value="16"></property> </bean> <!-- 构造方法注入(必须要有带参的构造方法) --> <!-- type是根据参数类型匹配方法 --> <bean id="s2" class="com.pojo.Student"> <constructor-arg value="张三" type="java.lang.String"></constructor-arg> <constructor-arg value="98" type="double"></constructor-arg> </bean> <!-- index可以调整参数的顺序 --> <bean id="s3" class="com.pojo.Student"> <constructor-arg value="张三" index="0"></constructor-arg> <constructor-arg value="98" index="2"></constructor-arg> <constructor-arg value="18" index="1"></constructor-arg> </bean> <!-- 如果value值中含有一些特殊符号可以使用 <![CDATA[符号]]>,但是value必须写成一个标签元素 --> <bean id="p1" class="com.pojo.Person"> <constructor-arg> <value><![CDATA[张三说&]]> </value> </constructor-arg> <constructor-arg value="三天不打,上房揭瓦" ></constructor-arg> </bean> <!-- 使用p命名空间进行属性注入(必须导入头文件xmlns:p="http://www.springframework.org/schema/p") --> <bean id="s4" class="com.pojo.Student" p:stuName="张三" p:age="18" p:score="100"></bean> <!-- 使用内部bean实现对象类型属性注入 --> <bean id="s5" class="com.pojo.Student"> <property name="stuName" value="陈帅"></property> <property name="age" value="16"></property> <property name="car"> <bean class="com.pojo.Car" p:brand="audi" p:price="300000" p:speed="260" p:safe="good"></bean> </property> </bean> <!-- 引用外部的bean实现对象类型属性注入 ,用ref="外部bean的id名"--> <bean id="c1" class="com.pojo.Car" p:brand="BMW" p:price="400000" p:speed="260" p:safe="good"></bean> <bean id="s6" class="com.pojo.Student"> <property name="stuName" value="陈帅"></property> <property name="age" value="16"></property> <property name="car" ref="c1"></property> </bean> <!-- 为级联属性赋值,注意:先要引用一个bean --> <bean id="c2" class="com.pojo.Car"></bean> <bean id="s7" class="com.pojo.Student"> <property name="stuName" value="lisi"></property> <property name="age" value="20"></property> <!-- 先引用外部的car对象 --> <property name="car" ref="c2"></property> <!-- 在修改car的属性值 --> <property name="car.brand" value="benc"></property> </bean> <!-- 注入list集合类型的属性(数组也是用list) --> <bean id="s8" class="com.pojo.Student" > <property name="phones"> <list> <value>iphone</value> <value>huawei</value> </list> </property> </bean> <!-- 注入set集合类型的属性 --> <bean id="s9" class="com.pojo.Student"> <property name="clos"> <set> <value>blue</value> <value>yellow</value> <value>red</value> </set> </property> </bean> <!-- 注入map集合类型的属性 --> <bean id="s10" class="com.pojo.Student"> <property name="map"> <map> <entry key="a1" value="123"></entry> <entry key="a2" value="456"></entry> <entry key="a3" value="789"></entry> </map> </property> </bean> <!-- 注入properties类型的属性 --> <bean id="dataSource" class="com.pojo.DataSource"> <property name="properties"> <props> <prop key="driver">com.mysql.jdbc.Driver</prop> <prop key="url">jdbc:mysql://localhost:3306/</prop> <prop key="username">root</prop> <prop key="password">123</prop> </props> </property> </bean> </beans>
-
- 使用注解方式进行IOC的配置,
-
@Component() 通用型
@Repository() 用于标注持久层(dao层) @Service() 用于标注业务类(service层) @Controller() 用于标注控制器类(controller层) @Autowired 根据类型自动装配,如果被装配的类型不是唯一的就要加@Qualifier("被装配的注解的名称") @Resource(name="被装配的注解的名称") 是根据名称去装配 -
package com.service; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import com.dao.UserDao; //service表示service层 //如果@Service()什么都不写,那就用使用该类的名称的首字母小写调用 @Service("userService") public class UserServiceImple implements UserService { //@Autowired是根据类型装配() @Autowired @Qualifier(value="userDao2")//这里也是名称 UserDao userDao; //这里的@Resource(name="userdao2")是根据名字去装配,如果这里不写,就是根据下面定义的dao这个名字去匹配 @Autowired @Resource(name="userDao2") UserDao dao; @Override public int save() { return userDao.save(); } }
- 使用IOC进行注入:
前提:先在applicationContext.xml文件中配置自动扫描:
<context:component-scan base-package="com.pojo,com.service,com.dao">
在需要配置的类上面:
@Component("stu")
表示注入一个组件,效果等同于在配置文件中加:
<bean id="stu" Class="com.pojo.Student">
</bean>
此时,就可以直接在测试类中通过getBean("stu")来获取当前对象。
用于表现层 用于业务层 用于持久化层
@Controller @Service @Repository
共性:都是@Componment的衍生注解,只是使用语义化的注解使得注入时更加的明确
最终要实现的效果:在各个层之间可以直接去调用和访问bean。
@AutoWired
作用:自动按照类型注入,只要容器中有唯一的类型匹配,则可以注入成功。如果没有匹配的类型,就会报错。
如何当前类型在容器中不唯一,如何保证注入成功???
@AutoWired
@Qualifier(value=””)进行配置,此时value可以配置当前对象的名称,从而实现按照名称来注入。
或者是:
直接使用:
@Resource
作用:直接按照bean的id进行注入
属性:name,用于指定bean的id
-
spring的IOC(控制反转),依赖注入
最新推荐文章于 2024-07-24 08:47:55 发布