当前位置: 首页 > news >正文

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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://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属性来指定

http://www.lryc.cn/news/477369.html

相关文章:

  • C#-类:成员属性
  • qt QDragEnterEvent详解
  • Vue项目与IE浏览器的兼容性分析(Vue|ElementUI)
  • 【C++之STL】一文学会使用 string
  • 好用的办公套件--- ONLYOFFICE
  • Android View事件分发
  • 攻防世界GFSJ1229 Three
  • 2023 icpc杭州(M,J,D,G,H)
  • 在CentOS 7上安装Alist
  • 【C/C++】memcpy函数的模拟实现
  • 嵌入式开发之线程互斥
  • JavaScript 变量作用域与函数调用机制:var 示例详解
  • Linux(CentOS)安装 JDK
  • AI产品经理实战手册:策略、开发与商业化指南
  • 【大语言模型】ACL2024论文-06 探索思维链COT在多模态隐喻检测中的应用
  • Linux之初体验
  • 现代化水电管理:Spring Boot在大学城的实践
  • 黑马官网2024最新前端就业课V8.5笔记---HTML篇
  • GS-Blur数据集:首个基于3D场景合成的156,209对多样化真实感模糊图像数据集。
  • Linux下Java的多种方式安装
  • Android Studio:connect time out
  • A014-基于Spring Boot的家电销售展示平台设计与实现
  • 数学期望和联合概率密度
  • 萤石私有化设备视频平台EasyCVR视频融合平台如何构建农业综合监控监管系统?
  • 【MongoDB】Windows/Docker 下载安装,MongoDB Compass的基本使用、NoSQL、MongoDB的基础概念及基础用法(超详细)
  • 微信小程序-自定义导航栏
  • vue中强制更新视图
  • mqsql 场景函数整理
  • 【AI日记】24.11.05 向量数据库 weaviate、混合搜索、多语言搜索、明确自己的南京
  • Scrapy入门