Spring
一、使用Spring创建对象
1.1 xml创建bean
想使用spring配置必须导入spring-context
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
-
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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
-
创建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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="com.betterAb.dto.Hello">
<property name="str" value="hello" />
</bean>
<!--bean标签中的name为别名,且能取多个-->
<bean id="hi" class="com.betterAb.dto.Hello" name="userOne,userFour">
<property name="str" value="hello2"/>
</bean>
<!--bean的别名-->
<alias name="hello" alias="userTwo"/>
<alias name="hi" alias="userThree"/>
<!--import导入多个xml,最后合并为一个bean的配置-->
<import resource="beans.xml"/>
</beans>
property:字段
name: 字段名
value:字段值
ref:引用(引用spring其他的bean)
-
实例化容器
//获取spring的上下文对象 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
1.2 依赖注入
-
准备
public class Address { private String location; public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } }
public class Student { private String name; private Address address; private String[] books; private List<String> hobbies; private Map<String,String> card; private Set<String> games; private String wife; private Properties info; ...(get(),set()) }
-
set注入
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.betterAb.dto.Student">
<!--1.普通值注入-->
<property name="name" value="张三"/>
<!--2.引用类型使用ref注入-->
<property name="address" ref="address"/>
<!--3.数组注入-->
<property name="books">
<array>
<value>三国演义</value>
<value>水浒传</value>
<value>西游记</value>
<value>红楼梦</value>
</array>
</property>
<!--4.List注入-->
<property name="hobbies">
<list>
<value>足球</value>
<value>篮球</value>
<value>乒乓球</value>
</list>
</property>
<!--5.Map注入-->
<property name="card">
<map>
<entry key="学生卡" value="1000449563"/>
<entry key="工卡" value="220373"/>
<entry key="身份证" value="620103199609200311"/>
</map>
</property>
<!--6.Set注入-->
<property name="games">
<set>
<value>荒野大镖客</value>
<value>英雄联盟</value>
<value>坦克世界</value>
</set>
</property>
<!--7.空值注入-->
<property name="wife">
<null/>
</property>
<!--8.Properties注入-->
<property name="info">
<props>
<prop key="学号">20190525</prop>
<prop key="性别">男性</prop>
<prop key="username">staff_id</prop>
<prop key="password">npmHR1236</prop>
</props>
</property>
</bean>
<bean id="address" class="com.betterAb.dto.Address">
<property name="location" value="杭州"/>
</bean>
</beans>
-
p标签,c标签注入
-
p标签注入(------>set注入)
<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" <------------p xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean name="user" class="com.betterAb.dto.User" p:mess="ok" p:password="npmHR1236" p:status="true" p:username="张三"/> </beans>
-
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" <-------------c xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="user" class="com.betterAb.dto.User" c:_0="张三" c:_1="npmHR1236" c:_2="ok" c:_3="false"/> </beans>
-
1.3 Bean作用域
1.4 Spring自动装配
- byName
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.betterAb.pojo.Student" autowire="byName">
<property name="name" value="张三"/>
</bean>
<bean id="address" class="com.betterAb.pojo.Address">
<property name="location" value="上海"/>
</bean>
</beans>
- byType
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.betterAb.pojo.Student" autowire="byType">
<property name="name" value="张三"/>
</bean>
//byType可以省略要装配的id
<bean class="com.betterAb.pojo.Address">
<property name="location" value="上海"/>
</bean>
</beans>