spring 初始IOC

初识spring

spring:是一个轻量级 的反控制(IOC) 和面向切面(AOP)的框架。
控制反转是一种设计思想(IOC),DI(依赖注入)是实现IOC的一种方式。
IOC是将有自己(程序员)创建的对象,控制反转转移给第三方(用户);
获得依赖注入的对象方式反转了;
控制反转时通过一种通过描述(XML或注解) 并通过第三生产获取特定的对象
的方式。在spring 中实现控制反转的时IOC容器,其实现方式时依赖注入(DependencyInjection,DI);

实例化对象

?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">
       <!--使用spring对象-->
     <bean id="hello" class="com.jbit.entitry.Hello">
         <!--属性-->
         <property name="Name" value="Spring"/>
     </bean>
</beans>
//获取spring的上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("baens.xml"); 
//提取对象 类型 Object 强制转换;
Hello h = (Hello) context.getBean("hello");
System.out.println(h.toString());

反转

控制反转的过程:
   控制:传传统应用的对象时有程序员来控制创建的,使用Spring之后,对象时有Spring来创建的;
   反转:程序本生不创建对象,变为被动接收对象;
   依赖注入: 就是利用set方法来进行注入;
   IOC时一种编程思想,有主动编程编程被动的接收;
   对象由Spring 来创建,管理,装配;

maven 依赖

     <!--spring maven 依赖-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-webmvc</artifactId>
           <version>5.2.2.RELEASE</version>
       </dependency>
       <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-jdbc</artifactId>
           <version>5.2.2.RELEASE</version>
       </dependency>

IOC 创建对象方式

1. 使用spring对象

   <!--方式1 使用spring对象 (无参构造)-->
     <bean id="hello" class="com.jbit.entitry.Hello">
         <!--set属性赋值-->
         <property name="Name" value="Spring"/>
     </bean>

2. 下标赋值

    <!--方式2 使用有参构造赋值-->
    <bean id="hello2" class="com.jbit.entitry.Hello">
        <!--下标赋值-->
         <constructor-arg index="0" value="狂神"/>
    </bean>

3.根据参数类型赋值

 <!--方式3 使用有参构造赋值 (不推荐使用)-->
    <bean id="hello3" class="com.jbit.entitry.Hello">
        <!--根据参数类型赋值-->
        <constructor-arg type="java.lang.String" value="狂神"/>
    </bean>

4.根据构造函数中参数

    <!--方式4 使用有参构造赋值-->
    <bean id="hello4" class="com.jbit.entitry.Hello">
        <!--直接更具构造函数中参数的名称赋值-->
        <constructor-arg name="name" value="狂神"/>
    </bean>

spring 配置

<!--别名 如果添加了别名也可以使用别名-->
    <alias name="hello" alias="h"/>
    <!--
id: bean 的对象名 唯一标识;
class: bean 的对应的全限定类名
name: 别名 (可以其多个别名)
-->   
<bean id="hello" class="com.jbit.entitry.Hello" name="别名1,别名2"></bean>
<!--Impot 一般作用于团队,可将多个配置文件,导入集合成一个配置;
使用总配置文件即可;-->
 <import resource="baens.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="addes" class="com.jbit.entitry.Addes">
        <property name="addresss" value="邵阳市邵东市宋家塘"/>
    </bean>
    <bean id="student" class="com.jbit.entitry.Student">
        <!--普通注入-->
     <property name="Name" value="佘莉"/>
        <!--bean注入-->
       <property name="addes" ref="addes"/>
        <!--s数组注入-->
        <property name="book">
            <array>
                <value>红楼梦</value>
                <value>三国演义</value>
                <value>水浒传</value>
                <value>西游记</value>
            </array>
        </property>
        <!--list集合注入-->
        <property name="list">
            <list>
                <value>玩游戏</value>
                <value>听歌</value>
            </list>
        </property>
        <!--map注入-->
        <property name="map">
            <map>
                <entry key="身份证" value="123166565656"/>
            </map>
        </property>
        <!--Set注入-->
        <property name="se">
            <set>
                <value>LOL</value>
                <value>CF</value>
            </set>
        </property>
        <!--Properties注入-->
        <property name="info">
             <props>
                 <prop key="学号">20200525123545</prop>
             </props>
        </property>
    </bean>
</beans>
<!--  P命名空间注入: p  - property 对应属性注入-->
<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        https://www.springframework.org/schema/beans/spring-beans.xsd">     <bean name="john-modern"        class="com.example.Person"        p:name="John Doe"        p:spouse-ref="jane"/>
</beans>
<!-- c命名空间注入: C -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
     <!-- c-namespace declaration with argument names -->
    <bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
        c:thingThree-ref="beanThree" c:email="something@somewhere.com"/>
</beans>

bean的作用域

范围	               描述
singleton      默认)将每个Spring IoC容器的单个bean定义范围限定为单个对象实例。

proptotype     将单个bean定义的作用域限定为任意数量的对象实例。

request        将单个bean定义的范围限定为单个HTTP请求的生命周期。也就是说,每个HTTP请求都有一个在单个bean 定义后面创建的bean实例。仅在可感知网络的Spring上下文中有ApplicationContext。

session       将单个bean定义的范围限定为HTTP的生命周期Session。仅在可感知网络的Spring上下文中有效 ApplicationContext。

apploctiong        将单个bean定义的作用域限定为的生命周期ServletContext。仅在可感知网络的Spring上下文中有效ApplicationContext。

webscoket    将单个bean定义的作用域限定为的生命周期WebSocket。仅在可感知网络的Spring上下文中有效 ApplicationContext。
<!--单例模式
 Spring(默认机制)scope="singleton"-->
 <bean id="accountService" class="com.something.DefaultAccountService"/><!-- the following is equivalent, though redundant (singleton scope is the default) --><bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>

<!--原型模式 
每次从容器中get的时候都会产生一个新的对象;scope="prototype"-->
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>

bean 自动装配

1.byName(自动装配)

<!--autowire="byName"

byName:会自动查找容器中和自己对象set方法后面相同的  bean id;

 注意: byName 的时候保证自己的属性于bean id 一致 并且唯一-->
<?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="peopole" class="com.jbit.entitry.Peopole" autowire="byName">
          <property name="name" value="佘莉"/>
      </bean>
</beans>

2.byType(自动装配)
autowire=“byType”
byType:会自动查找容器中和自己对象属性类型相同的 bean;
注意: byType 装配时 bean的类型唯一 并且以自己对象的属性的类型一致;

      <bean id="peopole" class="com.jbit.entitry.Peopole" autowire="byType">
          <property name="name" value="佘莉"/>
      </bean>

3.注解装配 @Autowired
1.导入约束 xmlns:context=“http://www.springframework.org/schema/context”
​ http://www.springframework.org/schema/context
​ https://www.springframework.org/schema/context/spring-context.xsd

2.配置注解支持 <context:annotation-config/>

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>

@Autowired :

可以在属性上直接使用
也可在set方法上使用

public class Peopole {
    private  String Name;
    @Autowired
    private Dog dog;
    @Autowired
    private Cat cat;
    @Autowired
    public void setName(String name) {
        Name = name;
    }
}

简单注解

<!-- 注解
@Nullable 该注解背后的参数可以未null
@Autowired(required = false)
Autowired 的属性required 为false 则允许为null,否则不允许为空,默认为true;
    
使用方法:(在类的属性或方法上)
@Autowired
@Qualifier(value = "cat")
Autowired 的装配环境比较复杂时 可以使用 Qualifier
@Qualifier(value = "cat") 去指定一个bean 进行装配


@Resource   先根据bean的 id 去查找 ,在根据类型去查找 id找不到时找 类型(必须唯一)
@Resource(name = "bean id") 更具 bean 的id去查找··
    
@Component(组件) 注解 :可将该类配置为容器中的bean
    衍生注解对应 MVC 三成架构
    dao         @Repository   
    serivce    @serivce
    contorller: @contorller
    
 @Scope("singleton")注解:作用域配置
   
-->
    <!--指定扫描的包-->
 <context:component-scan base-package="com.sli.entrtiy"/>
    在指定的包下的类中加入 @Component(组件) 注解 :类配置被Spring管理了 就是bean
    等同于 <bean id="user" class="com.sli.entrtiy.User"/>
    <!--作用域配置-->
<bean scope="singleton"/>

@Component 示例

@Component
public class User {
        @Value("张三")
    private  String Name ;

    public String getName() {
     return Name;
    }

    public void setName(String name) {
        Name = name;
    }
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值