Java Spring IOC控制反转创建对象的方式、Spring配置属性解释


控制反转

(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。


IOC创建对象的方式

pojo类

public class Hello {

    private String name;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Hello{" +
                "name='" + 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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--bean = 对象-->
    <!--id = 变量名-->
    <!--class = new的对象-->
    <!--property 相当于给对象中的属性设值-->
    <bean id="user" class="com.pojo.Hello">
        <property name="name" value="hellospring"/>
    </bean>
    
</beans>
	<bean id="userServiceImpl" class="com.service.UserServiceImp">
        <!--ref引用spring中已经创建很好的对象-->
        <!--value是一个具体的值-->
        <property name="userDao" ref="userdaomysql"/>
    </bean>
  • 有参构造的方式(3种)

下标赋值

<?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">

    <bean id="user" class="com.pojo.User">
        <constructor-arg index="0" value="hellospring"/>
    </bean>
</beans>

类型赋值(不建议使用)

<bean id="user" class="com.pojo.User">
    <constructor-arg type="java.lang.String" value="hellospring"/>
</bean>

直接通过参数名

<bean id="user" class="com.pojo.User">
    <constructor-arg name="name" value="hellospring"></constructor-arg>
</bean>

Test类

import com.hou.pojo.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest {

    public static void main(String[] args) {
        //获取spring上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
     	//beans.xml是spring配置文件
     	
        //我们的对象下能在都在spring中管理了,我们要使用,直接取出来就可以了
        Hello hello = context.getBean("hello",Hello.class);
        System.out.println(hello.toString());
    }
}

Spring配置

  • 别名alias
<bean id="user" class="com.hou.pojo.User">
    <constructor-arg name="name" value="hou"></constructor-arg>
</bean>

<alias name="user" alias="user2aaa"/>
  • Bean的配置

    name:别名,更高级,可以同时取多个别名。
    bean : 对象
    id : 变量名,bean的id标识符
    class : new的对象,bean对象所对应的类型
    property: 相当于给对象中的属性设值


    <bean id="user" class="com.pojo.Hello">
        <property name="name" value="hellospring"/>
    </bean>
    
  • import

一般用于团队开发,它可以将多个配置文件,导入合并为一个配置文件

<import resource="Springmvc.xml"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值