SpringIOC创建对象的三种方式
1、默认使用无参构造的创建对象
Hello.java:
public class Hello {
private String name;
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("Hello "+this.name);
}
}
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="cn.com.Ycy.spring.domain.Hello">
<property name="name" value="name"/>
</bean>
</beans>
测试类:
@Test
public void test02(){
ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Hello hello = (Hello) ac.getBean("hello");
hello.show();
}
如果写了有参构造方法(没写无参构造方法),这里的class就会爆红,类中没有无参构造方法,但是spring要调用无参构造方法。
2、我们要使用有参构造方法创建对象
1. 下标赋值
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="cn.com.Ycy.spring.domain.Hello">
<constructor-arg index="0" value="java"/>
</bean>
</beans>
2. 类型赋值(不好用,有两个相同的类型就会出现问题)
<?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="hello" class="cn.com.Ycy.spring.domain.Hello">
<constructor-arg type="java.lang.String" value="java"/>
</bean>
</beans>
3. 参数名name赋值
<?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="hello" class="cn.com.Ycy.spring.domain.Hello">
<constructor-arg name="name" value="spring"/>
</bean>
</beans>
3、使用普通工厂中的方法创建对象
使用某个类中的方法创建对象,并存入spring容器
接口:
public interface IAccountDao {
/** * 模拟保存账户 */
void saveAccount();
}
接口实现类:
public class AccountDaoImpl implements IAccountDao {
public AccountDaoImpl(){
System.out.println("AccountDaoIMpl对象创建了");
}
public void saveAccount(){
System.out.println("保存了账户");
}
}
普通工厂类:
public class beanFactory {
//工厂类-创建对象
public IAccountDao getAccountDao(){
return new AccountDaoImpl();
}
}
bean配置文件:
<?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 http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="beanfactory" class="com.Ycy.factory.beanFactory"></bean>-->
<bean id="accountDao" factory-bean="beanfactory" factory-method="getAccountDao"></bean>
</beans>
测试方法:
@Test
public void test01(){
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountDao accountDao = (IAccountDao) ac.getBean("accountDao");
System.out.println(accountDao);
accountDao.saveAccount();
}
4、使用工厂中的静态方法创建对象
使用某个类中的静态方法创建对象,并存入spring容器
工厂类:
public class staticFactory {
public static IAccountDao getAccountDao(){
return new AccountDaoImpl();
}
}
接口:
public interface IAccountDao {
/** * 模拟保存账户 */
void saveAccount();
}
接口实现类:
public class AccountDaoImpl implements IAccountDao {
public AccountDaoImpl(){
System.out.println("AccountDaoIMpl对象创建了");
}
public void saveAccount(){
System.out.println("保存了账户");
}
}
bean配置文件:
<?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 http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="accountDaoByStatic" class="com.Ycy.factory.staticFactory" factory-method="getAccountDao"></bean>
</beans>
测试方法:
/** * 静态工厂创建对象 */
@Test
public void test02(){
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountDao accountDao = (IAccountDao) ac.getBean("accountDaoByStatic");
System.out.println(accountDao);
accountDao.saveAccount();
}
创建对象
类型 变量名 = new 类型();
Hello hello = new Hello();
id = 变量名
class = new 的对象
property 相当于给对象中的属性设置一个值 —>对应的是set方法
没有set对应的方法是会报错的
在配置文件加载时,容器中的所有对象已经初始化了
还没有评论,来说两句吧...