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对应的方法是会报错的
在配置文件加载时,容器中的所有对象已经初始化了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值