Spring入门学习---02
Spring
1、Spring注入的具体实例
创建两个实体类
1、Address.class
package com.charles.pojo; public class Address { private String address; public Address(){} public Address(String address) { this.address = address; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Address{" + "address='" + address + '\'' + '}'; } }
2、Custom.class
package com.charles.pojo; import java.util.*; public class Custom { private String name; private Address address; private String[] books; private List<String> hobby; private Map<Integer,String> idCard; private Set<String> games; private String wife; private Properties info; public Custom(){} public Custom(String name, Address address, String[] books, List<String> hobby, Map<Integer, String> idCard, Set<String> games, String wife, Properties info) { this.name = name; this.address = address; this.books = books; this.hobby = hobby; this.idCard = idCard; this.games = games; this.wife = wife; this.info = info; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String[] getBooks() { return books; } public void setBooks(String[] books) { this.books = books; } public List<String> getHobby() { return hobby; } public void setHobby(List<String> hobby) { this.hobby = hobby; } public Map<Integer, String> getIdCard() { return idCard; } public void setIdCard(Map<Integer, String> idCard) { this.idCard = idCard; } public Set<String> getGames() { return games; } public void setGames(Set<String> games) { this.games = games; } public String getWife() { return wife; } public void setWife(String wife) { this.wife = wife; } public Properties getInfo() { return info; } public void setInfo(Properties info) { this.info = info; } @Override public String toString() { return "Custom{" + "name='" + name + '\'' + ", address=" + address + ", books=" + Arrays.toString(books) + ", hobby=" + hobby + ", idCard=" + idCard + ", games=" + games + ", wife='" + wife + '\'' + ", info=" + info + '}'; } }
3、ApplicationContext.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <bean id="address" class="com.charles.pojo.Address"> <property name="address" value="ShangHai"/> </bean> <bean id="custom" class="com.charles.pojo.Custom"> <property name="name" value="Charles"/> <property name="address" ref="address"/> <property name="books"> <array> <value>MYSQL</value> <value>DOCKER</value> <value>JAVA</value> </array> </property> <property name="hobby"> <list> <value>GAMING</value> <value>CODING</value> <value>DANCING</value> </list> </property> <property name="idCard"> <map> <entry key="1" value="441521"/> <entry key="2" value="20021116"/> </map> </property> <property name="games"> <set> <value>GTA</value> <value>APEX</value> <value>LOL</value> </set> </property> <property name="wife"> <null/> </property> <property name="info"> <props> <prop key="email">w1212133213@outlook.com</prop> <prop key="username">Charles_H</prop> <prop key="phone">110</prop> </props> </property> </bean> </beans>
4、进行测试
@Test public void test2(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println(context.getBean("custom").toString()); }
测试结果
2、IOC创建对象的方式
1、无参构造创建对象(默认方式)
2、有参构造:
1. 下标赋值
<constructor-arg index="0" value="Charles"/>
2. 类型赋值
<constructor-arg type="java.lang.String" value="Charles"/>
3. 直接通过参数名来赋值
<constructor-arg name="id" value="1"/> <constructor-arg name="username" value="Hello~"/>
3、关于Spring的配置
别名
<alias name="custom" alias="cs"/> <bean id="cs" class="com.charles.pojo.Custom"/>
import 用于合并配置文件
由于在实际开发当中,我们需要配置的东西非常多。因此,为了不弄混,我们的配置都是分开进行配置,最后在进行合并,最后只有调用该配置即可。
<?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"> <import resource="beans.xml"/> </beans>
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步