Spring 依赖注入二

其他注入方式

利用命名空间注入

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--通过p命名 空间注入 必须有get/set方法-->
    <bean id="girl" class="com.Girl" p:name="小红">

    </bean>
</beans>

注入null

<bean id="girl" class="com.Girl" >
            <property name="name">
                <null></null>
            </property>
</bean>

注入内部bean

<!--注入内部bean-->
        <property name="love" >
            <bean class="com.Girl">
                <property name="name" value="小花"></property>
            </bean>
        </property>

工厂bean 定义类型和返回类型不一致

public class Monster {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Monster{" +
                "name='" + name + '\'' +
                '}';
    }

    public Monster(String name) {
        this.name = name;
    }
}
import org.springframework.beans.factory.FactoryBean;

public class Mybean implements FactoryBean {
    //获取对象
    public Object getObject() throws Exception {
        Monster Monster =  new Monster("牛魔");
        return Monster ;
    }

    //获取对象类型
    public Class<?> getObjectType() {

        return Mybean.class;
    }

    //    是否单例
    public boolean isSingleton() {
        return true;
    }
}
<bean id="Mybean" class="com.Mybean">

</bean>

程序测试

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        //加载 spring配置文件
        ClassPathXmlApplicationContext xml =
                new ClassPathXmlApplicationContext("beans.xml");
        
        Monster hero = xml.getBean("Mybean", Monster.class);
        System.out.println(hero.getName());
    }
}

配置单例和多实例
spring 默认是单例模式

<!--prototype是多实例
    singleton 单例
-->
<bean id="girl" class="com.Girl" scope="singleton">
    
</bean>

引入外部属性文件

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/hero
jdbc.pwd=1234
jdbc.username=root
<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
  <!--导入外部配置文件-->
   <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
  
    <bean id="DataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${jdbc.url}"></property>
        <property name="driver" value="${jdbc.driver}"></property>
        <property name="password" value="${jdbc.pwd}"></property>
        <property name="username" value="${jdbc.username}"></property>
    </bean>

</beans>
posted @ 2022-06-07 15:05  起始者  阅读(16)  评论(0)    收藏  举报