Spring Ico

Spring Ico 的介绍:

Ico(Inverse of Control) 控制反转:将对象的创建和管理 交给第三方(Spring容器),从而降低了代码的耦合度。

Spring Ico 底层实现原理:通过反射机制原理,来实现对象的获取;

Ioc类型:构造函数注入,属性注入,接口注入,而Spring支持构造函数的注入和属性注入

构造函数注入:

public class User{

private String userName;
private int age;

//无参构造函数
public User(){}
//有参构造函数
public User(String name,int age){
      this.name=name;
      this.age=age;
}
...get/set函数...

}


<!--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.ecut.Ioc.User" >
         <constructor-agr index="0" value="xxx"/>
         <constructor-agr index="1" value=18/>
    </bean>
 </beans>

public class TestUser {

    public static void main(String[] args) {
//配置文件的绝对路径名
        String config="Ioc/User-config.xml";
//解析配置文件
        ApplicationContext context= new ClassPathXmlApplicationContext(config);
//获取对象
        User user1= context.getBean("user",User.class);
        System.out.println(user1);
       

    }

}

属性注入:通过SetXxx方式来注入Bean的属性或依赖(在平常的应用中,属性注入的方法用的更多).

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.ecut.entity.User">
        <property name="userName" value="李华"></property>
        <property name="age" value="19></property>
    </bean>
</beans>

静态工厂注入:

public class UseraStaticFactory {

//通过静态方法来获取对象
    public static User getUser(){
//返回对象
 return new User();
    }


}

<?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">
<!--factory-method   工厂类的方法-->
    <bean id="user1" class="com.ecut.Ioc.UseraStaticFactory" factory-method="getUser"/>

</beans>
非静态工厂注入
public class UseraStaticFactory {

//通过非静态方法来获取对象
    public  User getUser(){
        return new User();
    }


}
<?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="userFactory" class="com.ecut.Ioc.UserSaticFactory" >
    <bean id="user" factory-bean="userFactory"
           factory-method="getUser"/>

</beans>

Bean标签常用的属性:

id:名称(不能包含特殊的符号)

class:创建类的全路径名

name:名称(可以包含特殊的符号)

scope:Bean的作用范围(singleton,prototype,request,session,globalsession)

singleton:单列,表示只有一个对象(默认值)

prototype:多例,表示有多个对象

request:把创建的对象放入request域中

session:把创建的对象放入session域中

p命名空间注入:简化了xml文件的配置

<bean id="user" class="com.ecut.Ioc.User"
p:userName="xxx"
p:age="11"/>

集合类型注入:List,Set,Map,Properties

public Class User{

private List<String> like;
private Set<String> eat;
private Map<String,String>work;
private Properties Mail;
 .....
}

1>List

<bean id="user" class="com.ecut.User">

<proterty name="like" >

   <List>

           <value>打球</value>

            <value>骑车</value>

</List>

2>Set

<bean id="user" class="com.ecut.User">

<proterty name="eat">

<set>

    <value>水果</value>

    <value>蔬菜</value>

</set>
</proterty >
</bean>

3>Map

<bean id="user" class="com.ecut.User">

<proterty name="work">

<map>

   <entry>

        <key><value>打代码</value></key>

         <value>在办公室</value></key>

</entry>
 </map>
</proterty>
</bean>

4>Properties(Properties 类型可以看作Map类型)

<bean id="user" class="com.ecut.User>

<proterty name="mail">

<props>

    <prop key="jonMail">111***[email protected]</prop>

</props>

</proterty>

</bean>


基于注解配置Bean

@component

@componment(value="user")
//相当于配置文件中的<bean id="user" class="com.ecut.User"/>
 public Class User{
....
}

下面三个注解的功能和@component相同,但是它们表示的内容不同

@Repository:用于对Dao实现类进行标注.

@Service:用于对Service实现类进行标注.

@Controller:用于对Controller实现类进行标注.


扫描注解定义的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"
       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 http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 扫描类包以应用注解定义的Bean-->
    <context:component-scan base-package="com.ecut.annotationDI"/>
   
</beans>

自动装配Bean

@Autowired

Spring通过@Autowired注解现在Bean的依赖注入

@Resource :功能与@Autowired相识












猜你喜欢

转载自blog.csdn.net/weixin_42117006/article/details/80913879
ico