0%
Bean构造(使用无参构造器)
1
| <bean id="user" class="org.ljguo.pojo.User"/>
|
测试
1 2 3 4 5 6 7 8 9 10 11 12
| import org.ljguo.pojo.User; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:beans.xml"); User user = ac.getBean("user", User.class); System.out.println(user); } }
|
依赖注入
setter 注入
- 基本数据类型注入
name
value
1 2 3 4
| <bean id="user" class="org.ljguo.pojo.User"> <property name="age" value="221"/> <property name="username" value="ljguo"/> </bean>
|
- 引用类型注入
name
ref
1 2 3 4 5 6 7 8 9 10
| <bean id="user" class="org.ljguo.pojo.User"> <property name="age" value="221"/> <property name="username" value="ljguo"/> <property name="classes" ref="class"/> </bean>
<bean id="class" class="org.ljguo.pojo.Class" > <property name="className" value="hqyj" /> <property name="location" value="511" /> </bean>
|
- 使用
name
, name
表示构造器中形参的名字1 2 3 4 5
| <bean id="user" class="org.ljguo.pojo.User"> <constructor-arg name="username" value="ljguo" /> <constructor-arg name="age" value="21" /> <constructor-arg name="classes" ref="class"/> </bean>
|
- 使用
type
, type
表示构造器中形参的类型1 2 3 4 5
| <bean id="user" class="org.ljguo.pojo.User" > <constructor-arg type="java.lang.String" value="ljguo"/> <constructor-arg type="int" value="25"/> <constructor-arg type="org.ljguo.pojo.Class" ref="class"/> </bean>
|
- 使用
index
, index
表示构造器形参索引1 2 3 4 5
| <bean id="user" class="org.ljguo.pojo.User"> <constructor-arg index="0" value="ljguo"/> <constructor-arg index="1" value="26"/> <constructor-arg index="2" ref="class"/> </bean>
|
自动装配
集合注入
- pojo 实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| package org.ljguo.pojo;
import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set;
public class Collection { private int[] array; private List<String> list; private Set<String> set; private Map<String,String> map;
public int[] getArray() { return array; }
public void setArray(int[] array) { this.array = array; }
public List<String> getList() { return list; }
public void setList(List<String> list) { this.list = list; }
public Set<String> getSet() { return set; }
public void setSet(Set<String> set) { this.set = set; }
public Map<String, String> getMap() { return map; }
public void setMap(Map<String, String> map) { this.map = map; }
@Override public String toString() { return "Collection{" + "array=" + Arrays.toString(array) + ", list=" + list + ", set=" + set + ", map=" + map + '}'; } }
|
- XML 配置依赖注入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <bean id="collection" class="org.ljguo.pojo.Collection"> <property name="array"> <array> <value>1</value> <value>2</value> <value>3</value> </array> </property>
<property name="list"> <list> <value>ljguo</value> <value>gxx</value> <value>marlin</value> </list> </property>
<property name="map"> <map> <entry key="name" value="ljguo"/> <entry key="age" value="21"/> </map> </property>
<property name="set"> <set> <value>ljguo</value> <value>ljguo</value> <value>gxx</value> </set> </property> </bean>
|
- 测试类
1 2 3 4 5 6 7 8 9 10 11 12 13
| package org.ljguo.test;
import org.ljguo.pojo.Collection; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class CollectionTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:beans.xml"); Collection collection = ac.getBean("collection", Collection.class); System.out.println(collection); } }
|
小案例(链接数据库)
- 导入druid依赖坐标
1 2 3 4 5 6
| <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.16</version> </dependency>
|
- 配置xml依赖注入
1 2 3 4 5 6
| <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/ljguo"/> <property name="username" value="root"/> <property name="password" value="20001020"/> </bean>
|
- 测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package org.ljguo.test;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.sql.DataSource;
public class JdbcTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:application.xml"); DataSource dataSource = ac.getBean("dataSource", DataSource.class); System.out.println(dataSource); } }
|
使用 properties 配置文件
加载 properties 文件
- 开启名字空间
context
1 2 3 4 5 6 7 8 9
| <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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
|
- 使用
context
空间加载1
| <context:property-placeholder location="classpath*:*.properties"/>
|
这一行代码加载了所有的properties文件
properties 文件内容如下1 2 3 4
| jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ljguo jdbc.username=root jdbc.password=20001020
|
xml 配置文件可优化为如下内容1 2 3 4 5 6
| <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>
|
容器相关
- BeanFactory 是 IOC 容器的顶层接口, 初始化BeanFactory对象时, 加载的 bean 延迟加载
- ApplicationContext 接口是 Spring 容器的核心接口, 初始化时 bean 立即加载
- ApplicationContext 接口的常用初始化类
- ClassPathXmlApplicationContext
- FileSystemXmlApplicationContext