自动装配
| 根据指定装配规则(属性名称或者属性类型),Spring 自动将匹配的属性值进行注入 |
- 实现代码1:
byName根据属性名称注入 ,注入值bean的id值和类属性名称一样
| # 部门实体 |
| public class Dept { |
| |
| @Override |
| public String toString() { |
| return "Dept{}"; |
| } |
| |
| } |
| |
| # 员工实体 |
| public class Emp { |
| |
| private Dept dept; |
| |
| public void setDept(Dept dept) { |
| this.dept = dept; |
| } |
| |
| @Override |
| public String toString() { |
| return "Emp{" + |
| "dept=" + dept + |
| '}'; |
| } |
| |
| public void test() { |
| System.out.println(dept); |
| } |
| |
| } |
| |
| # 配置bean.xml |
| <bean id="emp" class="com.ychen.spring.autowire.Emp" autowire="byName"> |
| </bean> |
| |
| <bean id="dept" class="com.ychen.spring.autowire.Dept"></bean> |
| |
| # 测试方法 |
| @Test |
| public void test4() { |
| ApplicationContext context = |
| new ClassPathXmlApplicationContext("bean3.xml"); |
| Emp emp = context.getBean("emp", Emp.class); |
| System.out.println(emp); |
| } |
| |
| # 控制台 |
| Emp{dept=Dept{}} |
| |
| Process finished with exit code 0 |
| # 部门实体 |
| public class Dept { |
| |
| @Override |
| public String toString() { |
| return "Dept{}"; |
| } |
| |
| } |
| |
| # 员工实体 |
| public class Emp { |
| |
| private Dept dept; |
| |
| public void setDept(Dept dept) { |
| this.dept = dept; |
| } |
| |
| @Override |
| public String toString() { |
| return "Emp{" + |
| "dept=" + dept + |
| '}'; |
| } |
| |
| public void test() { |
| System.out.println(dept); |
| } |
| |
| } |
| |
| # 配置bean.xml |
| <bean id="emp" class="com.ychen.spring.autowire.Emp" autowire="byType"> |
| </bean> |
| |
| <bean id="dept" class="com.ychen.spring.autowire.Dept"></bean> |
| |
| # 测试方法 |
| @Test |
| public void test4() { |
| ApplicationContext context = |
| new ClassPathXmlApplicationContext("bean3.xml"); |
| Emp emp = context.getBean("emp", Emp.class); |
| System.out.println(emp); |
| } |
| |
| # 控制台 |
| Emp{dept=Dept{}} |
| |
| Process finished with exit code 0 |
byType缺陷:bean.xml中有2个bean时会报错

外部属性文件
-
导入依赖

-
直接配置
| <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> |
| <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> |
| <property name="url" value="jdbc:mysql://localhost:3306/dbtest"></property> |
| <property name="username" value="root"></property> |
| <property name="password" value="123456"></property> |
| </bean> |
使用context 名称空间
- src目录下创建jdbc.properties
| prop.driverClass=com.mysql.jdbc.Driver |
| prop.url=jdbc:mysql://localhost:3306/userDb |
| prop.userName=root |
| prop.password=root |
| <?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.xsd |
| http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> |
| |
| |
| <context:property-placeholder location="classpath:jdbc.properties"/> |
| |
| |
| <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> |
| <property name="driverClassName" value="${prop.driverClass}"></property> |
| <property name="url" value="${prop.url}"></property> |
| <property name="username" value="${prop.userName}"></property> |
| <property name="password" value="${prop.password}"></property> |
| </bean> |
| |
| </beans> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术