Spring bean学习

pom.xml中的配置

<dependencies>
    <!--dependency>
      <groupId>com.imooc</groupId>
      <artifactId>[the artifact id of the block to be mounted]</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <dependency>
      <groupId>jakarta.annotation</groupId>
      <artifactId>jakarta.annotation-api</artifactId>
      <version>1.3.5</version>
    </dependency>
  </dependencies>

Spring bean 传统通过xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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">
    <!-- UserService的创建权交给了Spring -->
<!--    <bean id="userService" class="com.imooc.ioc.demo1.UserServiceImpl">
        <property name="name" value="李四"/>
    </bean>-->

    <!--Bean的实例化的三种方式============================-->
    <!--第一种:无参构造器的方式-->
    <!--<bean id="bean1" class="com.imooc.ioc.demo2.Bean1"/>-->
    <!--第二种:静态工厂的方式-->
    <!--<bean id="bean2" class="com.imooc.ioc.demo2.Bean2Factory" factory-method="createBean2"/>-->
    <!--第三种:实例工厂的方式-->
    <!--<bean id="bean3Factory" class="com.imooc.ioc.demo2.Bean3Factory"/>
    <bean id="bean3" factory-bean="bean3Factory" factory-method="createBean3"/>-->

    <!--Bean的作用范围=====================================-->
    <!--<bean id="person" class="com.imooc.ioc.demo3.Person" scope="prototype"/>-->

   <!-- <bean id="man" class="com.imooc.ioc.demo3.Man" init-method="setup" destroy-method="teardown">
        <property name="name" value="张三"/>
    </bean>-->
<!--    <bean class="com.imooc.ioc.demo3.MyBeanPostProcessor"/>

    <bean id="userDao" class="com.imooc.ioc.demo3.UserDaoImpl"/>-->

    <!--Bean的构造方法的属性注入=============================-->
<!--    <bean id="user" class="com.imooc.ioc.demo4.User">
        <constructor-arg name="name" value="张三" />
        <constructor-arg name="age" value="23"/>
    </bean>-->

    <!--Bean的set方法的属性注入==============================-->
<!--    <bean id="person" class="com.imooc.ioc.demo4.Person">
        <property name="name" value="李四"/>
        <property name="age" value="32"/>
        <property name="cat" ref="cat"/>
    </bean>

    <bean id="cat" class="com.imooc.ioc.demo4.Cat">
        <property name="name" value="ketty"/>
    </bean>-->

    <!--Bean的p名称空间的属性注入==============================-->
    <bean id="person" class="com.imooc.ioc.demo4.Person" p:name="大黄" p:age="34" p:cat-ref="cat"/>

    <bean id="cat" class="com.imooc.ioc.demo4.Cat" p:name="小黄"/>

    <!--Bean的SpEL的属性注入==============================-->
    <bean id="category" class="com.imooc.ioc.demo4.Category">
        <property name="name" value="#{'服装'}"/>
    </bean>

    <bean id="productInfo" class="com.imooc.ioc.demo4.ProductInfo"/>

    <bean id="product" class="com.imooc.ioc.demo4.Product">
        <property name="name" value="#{'男装'}"/>
        <property name="price" value="#{productInfo.calculatePrice()}"/>
        <property name="category" value="#{category}"/>
    </bean>

    <!--集合类型的属性注入=================================-->
    <bean id="collectionBean" class="com.imooc.ioc.demo5.CollectionBean">
        <!--数组类型-->
        <property name="arrs">
            <list>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </list>
        </property>
        <!--List集合的属性注入-->
        <property name="list">
            <list>
                <value>111</value>
                <value>222</value>
                <value>333</value>
            </list>
        </property>
        <!--Set集合的属性注入-->
        <property name="set">
            <set>
                <value>ddd</value>
                <value>eee</value>
                <value>fff</value>
            </set>
        </property>
        <!--Map集合的属性注入-->
        <property name="map">
            <map>
                <entry key="aaa" value="111"/>
                <entry key="bbb" value="222"/>
                <entry key="ccc" value="333"/>
            </map>
        </property>
        <!--Properties的属性注入-->
        <property name="properties">
            <props>
                <prop key="username">root</prop>
                <prop key="password">1234</prop>
            </props>
        </property>
    </bean>
</beans>

Spring bean注解配置:

https://www.cnblogs.com/l-y-h/p/11342174.html

就是记得在xml中配置扫描

    <!--开启注解扫描=======================-->
 <context:component-scan base-package="com.weon"/>
	<!--或者-->
 <!--<context:annotation-config/>-->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值