IOC
控制反转,将bean交给spring容器管理。
P注入_xml
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
<bean id="school" class="bean.School" p:sid="1"
p:stu-ref="student">
<!-- <property name="stu">-->
<!-- <ref bean = "student" />-->
<!-- </property>-->
</bean>
</beans>
set注入_构造器注入
<bean id = "student" class = "bean.Student">
<constructor-arg name = "sid" value = "1"></constructor-arg>
<constructor-arg name="name" value = "zhangsan"></constructor-arg>
<constructor-arg name = "age" value="20"></constructor-arg>
<constructor-arg name = "course" value = "Java"></constructor-arg>
</bean>
<bean id="teacher" class="bean.Teacher">
<property name="tid" value="1">
</property>
<property name="name" value="lisi"></property>
</bean>
<bean id="mix" class="bean.Mix">
<property name="myList">
<list>
<value>list01</value>
<value>list02</value>
</list>
</property>
<property name="myMap">
<map>
<entry key = "key01" value="map01"></entry>
<entry key = "key02" value="map02"></entry>
</map>
</property>
<property name="myArray">
<array>
<value>array01</value>
<value>array02</value>
</array>
</property>
</bean>
属性注入
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="student" class="com.kuang.pojo.Student">
<property name="name" value="三线码农学编程"></property>
<property name="books">
<array>
<value>java核心技术</value>
<value>深入了解java虚拟机</value>
<value>sql必知必会</value>
</array>
</property>
<property name="address" ref="address"></property>
<property name="hobbys">
<list>
<value>code</value>
<value>play</value>
<value>run</value>
</list>
</property>
<property name="card">
<map>
<entry key="身份证" value="2001"></entry>
<entry key="银行卡" value="0624"></entry>
</map>
</property>
<property name="wife" >
<null></null>
</property>
<property name="info">
<props>
<prop key="学号">201912984</prop>
<prop key="性别">男</prop>
</props>
</property>
</bean>
<bean id="address" class="com.kuang.pojo.Address">
<property name="address" value="武汉"></property>
</bean>
</beans>
常用依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
注解说明
@Autowired : 自动装配通过类型,名字;默认Bytype
@Resource: 通过Byname 的方式实现
@Component : 类被管理 组件放在类上说明这个类被spring管理
@repository dao
@service service
@controller controller
@Value : 注入值
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config></context:annotation-config>
</beans>
衍生的注解
@Component 衍生注解
- dao【@Repostory】
- service【@Service】
- controller【@controller】
注解注入属性 xml管理bean