Spring-Day4

12.HelloSpring

复制代码
<?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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<!--使用spring来创建对象,在spring这些都称为bean
    类型 变量名 = new 类型();
    Hello hello = new Hello();
​
    id = 变量名
    class = new 的对象
    property 相当于给对象中是属性设置一个值
​
-->
​
    <bean id="hello" class="com.itheima.App">
        <property name="str" value="Spirng"/>
    </bean>
</beans>

接下来,将对象取出来

复制代码
public class MyTest {
    public static void main(String[] args) {
        //获取Spring的上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //我们的对象都在Spring中管理了,要使用去里面取
        App hello =(App)context.getBean("hello");
        System.out.println(hello.toString());
    }
}

通过spring创建对象

复制代码
<?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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
​
    <bean id="mysqlImpl" class="com.itheima.dao.UserDaoMysqlImpl"/>
    <bean id="UserServiceImpl" class="com.itheima.service.UserServiceImpl">
<!--
      ref:引用spring容器中创建好的对象
      value:具体的值,基本数据类型
      -->
        <property name="userDao" ref="mysqlImpl"/>
    </bean>
</beans>
          
          
     
//在Mytest中取出
          public class MyTest {
    public static void main(String[] args) {
        //获取ApplicationContext:拿到Spring 的容器
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //容器有了之后,需要什么get什么
        UserServiceImpl userService = (UserServiceImpl) context.getBean("UserServiceImpl");
        UserServiceImpl.getUser();
    }
}

到了现在,我们彻底不用在程序中去改动了,要实现不同的操作,只需要在xml配置文件中修改,所谓的Ioc,一句话就是:对象由Spring来创建,管理,装配!

13.IOC创建对象的方式

  1. 使用无参构造创建对象,默认

  2. 假设要用有参构造创建对象

    复制代码
    //1.下标赋值
    <bean id="user" class="com.itheima.pojo.User">
        <constructor-arg index="0" value="Zkw"/>
    </bean>
        
    ​
    //2.通过类型创建(不建议使用)
    <bean id="user" class="com.itheima.pojo.User">
        <constructor-arg type="java.lang.String" value="Zkw"/>
    </bean>
        
        
    //3.通过参数名
    <bean id="user" class="com.itheima.pojo.User">
        <constructor-arg name="name(参数名)" value="Zkw"/>
    </bean>

总结:在配置文件加载的时候,容器中管理的对象就已经初始化了

13.注解实现自动装配

  1. 导入约束:context约束

  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"
    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>
        
        
//具体的修改方法:
  //将xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"修改为
  //xmlns:context="http://www.springframework.org/schema/context"
  //然后复制后两行,把beans改成context即可,aop导入也是如此

@Autowired

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

使用Autowired 我们可以不用编写set方法了,前提是自动装配属性在IOC容器中存在,且名字正确

可以配合**@Qualifier(value="")**来指定名字

@Resource也可以,它会先查找名字,没有对应名字再去找对应的类型,也可以加个name属性来指定

相关推荐
我是福福大王6 分钟前
MyBatis源码学习总结
后端·mybatis
玄明Hanko16 分钟前
生产环境到底能用Docker部署MySQL吗?
后端·mysql·docker
sayornottt17 分钟前
Rust中的动态分发
后端·rust
ApeAssistant18 分钟前
Spring + 设计模式 (十四) 行为型 - 观察者模式
spring·设计模式
创码小奇客19 分钟前
MongoDB 时间序列:解锁数据时光机的终极指南
java·mongodb·trae
黯_森19 分钟前
Java面向对象
java·后端
小厂永远得不到的男人19 分钟前
WebSocket深度剖析:实时通信的终极解决方案实践指南
后端·websocket
代码小侦探21 分钟前
Java中以Maven方式引入Oracle JDBC Driver依赖的详解
java·oracle·maven
不畏惧的少年21 分钟前
AQS的底层实现原理
java
ApeAssistant22 分钟前
Spring + 设计模式 (十三) 行为型 - 策略模式
spring·设计模式