先导,对IOC容器的理解
- 通俗的讲就是把你的class类交给spring的IOC容器去管理
- 需要对该类的属性注入一些值,就可以通过spring提供的xml文件或者注解进行注入
- 自己使用时在IOC容器工厂中去获取就可以了,从而实现控制
1、导入maven依赖 5.3.15版本
| <?xml version="1.0" encoding="UTF-8"?> |
| <project xmlns="http://maven.apache.org/POM/4.0.0" |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| <parent> |
| <artifactId>pro01-maven-ider-parent</artifactId> |
| <groupId>com.mhy.maven</groupId> |
| <version>1.0-SNAPSHOT</version> |
| </parent> |
| <modelVersion>4.0.0</modelVersion> |
| |
| <artifactId>pro06-module-spring</artifactId> |
| |
| <dependencies> |
| |
| <dependency> |
| <groupId>junit</groupId> |
| <artifactId>junit</artifactId> |
| <version>4.12</version> |
| <scope>test</scope> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-webmvc</artifactId> |
| <version>5.3.15</version> |
| </dependency> |
| |
| </dependencies> |
| |
| </project> |
2、xml配置文件的常用bean注入
| public class Student { |
| |
| private String name; |
| private Address address; |
| private String[] books; |
| private List<String> hobbys; |
| private Map<String,String> card; |
| private Set<String> games; |
| private Properties info; |
| private String wife; |
| } |
| <?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="address" class="com.mhy.pojo.Address"> |
| <property name="address" value="重庆交通大学"/> |
| </bean> |
| <bean id="student" class="com.mhy.pojo.Student"> |
| |
| <property name="name" value="mhy"/> |
| <property name="address" ref="address"/> |
| <property name="books"> |
| <array> |
| <value>西游记</value> |
| <value>红楼梦</value> |
| <value>水浒传</value> |
| <value>三国演义</value> |
| </array> |
| </property> |
| <property name="hobbys"> |
| <list> |
| <value>数学</value> |
| <value>代码</value> |
| </list> |
| </property> |
| <property name="games"> |
| <set> |
| <value>LOL</value> |
| <value>CF</value> |
| <value>DMF</value> |
| </set> |
| </property> |
| <property name="card"> |
| <map> |
| <entry key="身份证" value="5002372222287878787"/> |
| <entry key="学生卡" value="631910040417"/> |
| </map> |
| </property> |
| <property name="info"> |
| <props> |
| <prop key="driver">com.mysql.jdbc.Driver</prop> |
| <prop key="url">url=jdbc:mysql://localhost:3306/smbms?characterEncoding=utf8</prop> |
| <prop key="username">root</prop> |
| <prop key="password">123456</prop> |
| </props> |
| </property> |
| <property name="wife"> |
| <null/> |
| </property> |
| |
| </bean> |
| |
| </beans> |
| @Test |
| public void testSpring2(){ |
| ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml"); |
| Student student = context.getBean("student", Student.class); |
| System.out.println(student); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| <?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:c="http://www.springframework.org/schema/c" |
| xsi:schemaLocation="http://www.springframework.org/schema/beans |
| https://www.springframework.org/schema/beans/spring-beans.xsd"> |
| |
| <bean id="hello" class="com.mhy.pojo.Hello"> |
| <property name="str" value="hello spring"/> |
| </bean> |
| <bean id="hello4" class="com.mhy.pojo.Hello"> |
| <property name="str" value="hello spring 2"/> |
| </bean> |
| <bean id="hello2" class="com.mhy.pojo.Hello" p:str="hello spring p"/> |
| <bean id="hello3" class="com.mhy.pojo.Hello" c:str="hello spring c"/> |
| |
| </beans> |
注意:
- c标签是set注入
- p标签是有参数构造注入
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)