spring _yc

IOC

控制反转:对象由spring来创建,管理,和装配。

  1. 创建对象
    先创建beans.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="teachen" class="com.myc.pojo.Teacher">
            <property name="student" ref="student"></property>
            <property name="name" value="myc"></property>
        </bean>
       <bean id="student" class="com.myc.pojo.Student">
           <property name="name" value="sadsa"></property>
           <property name="age" value="15"></property>
       </bean>
</beans>

再创建ClassPathXmlApplicationContext对象

  @Test
    public void test1()
    {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Teacher teacher =(Teacher) classPathXmlApplicationContext.getBean("teachen");
        System.out.println(teacher);
    }

创建出了teacher对象,实现spring的控制反转
其他标签
创建对象也可以使用name
在这里插入图片描述
alias表示给对象取别名

DI属性注入

  1. set注入参考以上ioc中使用property会使用实体类中的set方法注入属性,当实体类中有无参构造器时可以创建对象,当有参构造时必须使用创建对象
    报错:报错信息

使用constructor-arg
第一种使用index表示构造器中参数的顺序注入
在这里插入图片描述
第二种使用普通name
在这里插入图片描述
其他类型参数注入
在这里插入图片描述

<bean name="myTeacher" class="com.myc.pojo.Teacher">
            <constructor-arg name="name"  value="黎明"> </constructor-arg>
            <constructor-arg name="saying" value="好好学习"></constructor-arg>
            <property name="books">
                <array>
                    <value>水浒传</value>
                    <value>红楼梦</value>
                </array>
            </property>
            <property name="list">
                <list>
                    <value>哈哈 </value>
                    <ref bean="student2"></ref>
                </list>
            </property>
            <property name="map">
                <map>
                    <entry key="" value="1"></entry>
                </map>
            </property>
            <property name="set">
                <set>
                    <value>11</value>
                    <value>11</value>
                    <value>22</value>
                </set>
            </property>
            <property name="properties">
                <props>
                    <prop key="A">a</prop>
                    <prop key="B">b</prop>
                </props>
            </property>

        </bean>

C命名空间注入—可以代替构造器参数注入
需要导入约束

P空间注入—可以代替属性property注入
导入约束
在这里插入图片描述
在这里插入图片描述
这里的p会覆盖C的值

bean作用域

在这里插入图片描述

把这个对象变成单例 ,不利于多线程会修改值。

@Autowired

自动装配
需要导入约束
在这里插入图片描述
导入约束并且使用注解
在实体类中使用 @Autowired

在这里插入图片描述
在beans.xml中配置实体类
在这里插入图片描述
byName会自动查找xml中student与teacher相同名字的类装配到属性中。
在这里插入图片描述
在这里插入图片描述
以上可创建people类中属性teacher与student中装配到值。

@Component

将对象交给spring去管理与Autowired区别为不需要再在beans.xml写配置对象的bean标签
第一步需要导入aop约束
开启注解扫描

<context:component-scan base-package="com.myc"/>
<context:annotation-config/>

component-scan会扫描路径下的注解
衍生注解作用与componment一样

@Component
public class User implements Serializable {
    @Value("mmyc")
    private String userName;
}

@Value注入属性相当于xml文件中bean标签里property 还可以注入propertis里内容
properties

password=root

给对象注入属性

@Component("c3p0")
public class C3P0Demo {
    @Value("${password}")
    private String username;

调用

    @Autowired
    @Qualifier("c3p0")
    private C3P0Demo c3P0Demo;

    @RequestMapping("/hello")
    public String hello()
    {
        System.out.println(c3P0Demo);
        return "succ";
    }

@Qualifier(“c3p0”)表示找容器中ID为c3p0的对象注入到属性中。结合Autowired一起使用

dao层注解:@Repository
service层注解:@Service
Controller层注解:@Controller

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值