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属性来指定

相关推荐
森林-2 分钟前
MyBatis 从入门到精通(第一篇)—— 框架基础与环境搭建
java·tomcat·mybatis
正在走向自律12 分钟前
Java连接电科金仓数据库(KingbaseES)实战指南
java·数据库·oracle·国产数据库·kingbase
xiaoye370818 分钟前
Java 事务失效场景全解析
java
weixin_4365250733 分钟前
Spring Boot 集成 EasyExcel 的最佳实践:优雅实现 Excel 导入导出
java·spring boot·后端
ChinaRainbowSea36 分钟前
9. LangChain4j + 整合 Spring Boot
java·人工智能·spring boot·后端·spring·langchain·ai编程
ゞ 正在缓冲99%…38 分钟前
leetcode35.搜索插入位置
java·算法·leetcode·二分查找
武昌库里写JAVA1 小时前
Mac下Python3安装
java·vue.js·spring boot·sql·学习
shengjk11 小时前
一文搞懂 Flink2.x 分离式状态管理
后端
程序员清风1 小时前
滴滴三面:ZGC垃圾收集器了解吗?
java·后端·面试
WWZZ20251 小时前
视觉SLAM第10讲:后端2(滑动窗口与位子图优化)
c++·人工智能·后端·算法·ubuntu·机器人·自动驾驶