先添加jar包
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!--spring-core-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<!--spring-bean-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<!--spring-context-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!--spring-expression-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</dependency>
</dependencies>
第二步创建配置文件spring.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 http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
第三步书写一个类User
package com.ioc11.entity;
import java.io.IOException;
import java.util.Properties;
/**
* package_name:com.ioc11.entity
* Author:徐亚远
* Date:2020/1/29 16:04
* 项目名:springDemo01
* Desription:
**/
public class User {
private String username;
private String password;
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void init(){
this.username = reveale(username);
this.password = reveale(password);
}
private String reveale(String value) {
if (value==null||"".equals(value)){
return value;
}
if (value.startsWith("${")&&value.endsWith("}")){
String key = value.substring(2,value.length()-1 );
return getproperty(key);
}
return value;
}
private String getproperty(String key) {
Properties properties = new Properties();
try {
properties.load(User.class.getClassLoader().getResourceAsStream("ioc11/info.properties"));
} catch (IOException e) {
e.printStackTrace();
}
if (properties.containsKey(key)){
return properties.getProperty(key);
}else {
throw new RuntimeException("not found key:"+key+"in this properties");
}
}
}
第四步 配置spring.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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--通过静态工厂方式有参的-->
<bean id="user" class="com.ioc11.entity.UserBeanFactory" init-method="init" factory-method="getSpingBean">
<constructor-arg name="username" value="${username}"/>
<constructor-arg name="password" value="${password}"/>
</bean>
</beans>
配置info.properties文件
username=admin
password=admin
书写UserBeanFactory类
package com.ioc11.entity;
/**
* package_name:com.ioc11.entity
* Author:徐亚远
* Date:2020/1/29 16:29
* 项目名:springDemo01
* Desription:
**/
public class UserBeanFactory {
/*通过静态工厂方式 有参的*/
public static User getSpingBean(String username, String password) {
User user = new User();
user.setUsername(username);
user.setPassword(password);
System.out.println("通过静态工厂方式有参的");
return user;
}
}
第五步书写测试方法UserController类
package com.ioc11.controller;
import com.ioc11.entity.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* package_name:com.ioc11.controller
* Author:徐亚远
* Date:2020/1/29 16:08
* 项目名:springDemo01
* Desription:
**/
public class UserController {
public static void main(String args[]) {
ApplicationContext ac = new ClassPathXmlApplicationContext("ioc11/spring.xml");
User user = (User) ac.getBean("user");
System.out.println("user:"+user);
}
}
执行结果如图: