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

包装设计网站设计平台潍坊网站外包

包装设计网站设计平台,潍坊网站外包,深圳观澜网站建设,pta程序设计平台目录 IOC接口ApplicationContext 详解IOC操作Bean管理基于xml方式基于xml方式创建对象基于xml方式注入属性使用set方法进行注入通过有参数的构造进行注入p 名称空间注入(了解) 基于xml方式注入其他类型属性xml 注入数组类型属性 IOC接口 IOC思想基于IOC…

目录

  • IOC接口
    • ApplicationContext 详解
      • IOC操作Bean管理
        • 基于xml方式
          • 基于xml方式创建对象
          • 基于xml方式注入属性
            • 使用set方法进行注入
            • 通过有参数的构造进行注入
            • p 名称空间注入(了解)
          • 基于xml方式注入其他类型属性
          • xml 注入数组类型属性

IOC接口

  • IOC思想基于IOC容器完成,IOC容器底层就是对象工厂
  • Spring提供IOC容器实现的两种方式:
    • ApplicationContext :BeanFactory接口的子接口,提供更多更强大的功能,一般由开发人员使用 在这里插入图片描述
    • BeanFactory :IOC容器基本实现,是Spring里面内部使用接口,不提供开发人员进行使用在这里插入图片描述
    • BeanFactory 在加载配置文件的时候,它不会把里面的对象创建,它只会加载我们的配置文件,当在获取的时候它才会创建对象。什么时候用的时候才开始创建对象
    • ApplicationContext 在加载配置文件的时候,就会把配置文件对象进行创建

ApplicationContext 详解

在这里插入图片描述

  • FileSystemXmlApplicationContext : 配置文件为系统盘的文件
  • ClassPathXmlApplicationContext : 配置为类路径下的文件

IOC操作Bean管理

  • Bean管理指的是两个操作
    • ① Spring创建对象 :Spring通过Xml解析再通过工厂模式来进行创建对象
    • ② Spring 注入属性 :Spring可以使用类似于Set对象的操作对对象或者字注入值
  • Bean管理操作有两种方式
    • ①基于xml配置文件方式实现
    • ②基于注解方式实现
基于xml方式
基于xml方式创建对象

在这里插入图片描述
① 在spring配置文件中,使用bean标签,标签里面添加对应属性,就可以实现对象创建
② 在bean标签有很多属性,介绍常用的属性

  • id属性 : 获取对象中的唯一的标识
  • class属性 :类全路径(包类路径)
  • name属性 :name的作用与id一样,name属性可以添加特殊符号
    ③ 创建对象的时候,默认也是执行无参的构造方法完成对象的创建
基于xml方式注入属性

DI : 依赖注入,就是注入属性

使用set方法进行注入

在这里插入图片描述
在spring配置文件配置对象创建,配置属性注入(使用Set方式注入)
在这里插入图片描述

通过有参数的构造进行注入

在这里插入图片描述
在spring配置文件配置对象创建,配置属性注入(使用有参数构造注入)
在这里插入图片描述

p 名称空间注入(了解)

使用 p 名称空间注入,可以简化基于 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"xmlns: p="http: //www. springframework. org/schema/p  //添加 p 名称空间在配置文件中
http://www.springframework.org/schema/beans/spring-beans.xsd">//进行属性注入,在 bean 标签里面进行操作<bean id="book" class="com. atguigu. spring5.Book" p:bname="九阳神功"
p: bauthor="无名氏">×/bean></beans>
基于xml方式注入其他类型属性
  1. 字面量
  • null 值
<property name="address"><null/>
</property> 
  • 属性值包含特殊符号
<!--属性值包含特殊符号1 把<>进行转义 &lt; &gt;2 把带特殊符号内容写到 CDATA
-->
<property name="address"><value><![CDATA[<<南京>>]]></value>
</property>
  1. 注入属性-外部 bean (外部调用对象的方法)
    • 创建两个类 ,service 类和 dao 类
    • 在 service 调用 dao 里面的方法
    • 在 spring 配置文件中进行配置
public class UserService {//创建UserDao类型属性,生成set方法private UserDao userDao;public void setUserDao(UserDao userDao) {this.userDao = userDao;}public void add() {System.out.println("service add...............");userDao.update();}
}
public class UserDao {public void update() {System.out.println("dao update...........");}
}
    • 通过springxml配置操作
<?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"><!--1 service和dao对象创建--><bean id="userService" class="com.atguigu.spring5.service.UserService"><!--注入userDao对象name属性:类里面属性名称ref属性:创建userDao对象bean标签id值--><property name="userDao" ref="userDaoImpl"></property></bean><bean id="userDaoImpl" class="com.atguigu.spring5.dao.UserDaoImpl"></bean>
</beans>
@Test
public void testBean1() {//1 加载spring配置文件ApplicationContext context =new ClassPathXmlApplicationContext("bean2.xml");//2 获取配置创建的对象UserService userService = context.getBean("userService", UserService.class);userService.add();}
  1. 注入属性-内部 bean (内部属性包含对象)

① 一对多关系:部门和员工一个部门有多个员工,一个员工属于一个部门部门是一,员工是多
②在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示

部门类

//部门类
public class Dept {private String dname;public void setDname(String dname) {this.dname = dname;}@Overridepublic String toString() {return "Dept{" +"dname='" + dname + '\'' +'}';}
}

员工类

//员工类
public class Emp {private String ename;private String gender;//员工属于某一个部门,使用对象形式表示private Dept dept;//生成dept的get方法public Dept getDept() {return dept;}public void setDept(Dept dept) {this.dept = dept;}public void setEname(String ename) {this.ename = ename;}public void setGender(String gender) {this.gender = gender;}public void add() {System.out.println(ename+"::"+gender+"::"+dept);}
}
<?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--><bean id="emp" class="com.atguigu.spring5.bean.Emp"><!--设置两个普通属性--><property name="ename" value="lucy"></property><property name="gender" value=""></property><!--设置对象类型属性--><property name="dept"><bean id="dept" class="com.atguigu.spring5.bean.Dept"><property name="dname" value="安保部"></property></bean></property></bean>
</beans>
    @Testpublic void testBean2() {//1 加载spring配置文件ApplicationContext context =new ClassPathXmlApplicationContext("application.xml");//2 获取配置创建的对象Emp emp = context.getBean("emp", Emp.class);emp.add();}

在这里插入图片描述

  1. 注入属性-级联赋值 (都可以达到注入的作用)

第一种方法 直接赋值一个对象

<!--级联赋值--> 
<bean id="emp" class="com.atguigu.spring5.bean.Emp"><!--设置两个普通属性--><property name="ename" value="lucy"></property><property name="gender" value=""></property><!--级联赋值--><property name="dept" ref="dept"></property>
</bean>
<bean id="dept" class="com.atguigu.spring5.bean.Dept"><property name="dname" value="财务部"></property>
</bean>

第二种方法 赋值里面的一个属性值
//生成dept的get方法

public Dept getDept(){
return dept:
}
<!--级联赋值--> 
<bean id="emp" class="com.atguigu.spring5.bean.Emp"><!--设置两个普通属性--><property name="ename" value="lucy"></property><property name="gender" value=""></property><!--级联赋值--><property name="dept" ref="dept"></property><property name="dept.dname" value="技术部"></property>
</bean> 
<bean id="dept" class="com.atguigu.spring5.bean.Dept"><property name="dname" value="财务部"></property>
</bean>
xml 注入数组类型属性
  1. 注入数组类型属性
    创建类,定义数组、list、map、set 类型属性,生成对应 set 方法
package com.tde.example.Entity;import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;public class Student {//1 数组类型属性private String[] courses;//2 list集合类型属性private List<String> list;//3 map集合类型属性private Map<String, String> maps;//4 set集合类型属性private Set<String> sets;public void setSets(Set<String> sets) {this.sets = sets;}public void setCourses(String[] courses) {this.courses = courses;}public void setList(List<String> list) {this.list = list;}public void setMaps(Map<String, String> maps) {this.maps = maps;}public void test() {System.out.println(Arrays.toString(courses));System.out.println(list);System.out.println(maps);System.out.println(sets);}
}
<?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"><!--1 集合类型属性注入--><bean id="stu" class="com.atguigu.spring5.collectiontype.Stu"><!--数组类型属性注入--><property name="courses"><array><value>java课程</value><value>数据库课程</value></array></property><!--list类型属性注入--><property name="list"><list><value>张三</value><value>小三</value></list></property><!--map类型属性注入--><property name="maps"><map><entry key="JAVA" value="java"></entry><entry key="PHP" value="php"></entry></map></property><!--set类型属性注入--><property name="sets"><set><value>MySQL</value><value>Redis</value></set></property></bean>
</beans>
@Test
public void testCollection1() {ApplicationContext context =new ClassPathXmlApplicationContext("bean5.xml");Stu stu = context.getBean("stu", Stu.class);stu.test();
}

在这里插入图片描述
2. 在集合里面设置对象类型值

<!--创建多个 course 对象--> 
<bean id="course1" class="com.atguigu.spring5.collectiontype.Course"><property name="cname" value="Spring5 框架"></property>
</bean> 
<bean id="course2" class="com.atguigu.spring5.collectiontype.Course"><property name="cname" value="MyBatis 框架"></property>
</bean>
<!--注入 list 集合类型,值是对象--><property name="courseList"><list><ref bean="course1"></ref><ref bean="course2"></ref></list>
</property>
//学生所学多门课程
private List<Course> courseList;
public void setCourseList(List<Course> courseList) {this.courseList = courseList;
}

在这里插入图片描述

  1. 把集合注入部分提取出来
  • 在 spring 配置文件中引入名称空间 util
  • 使用 util 标签完成 list 集合注入提取

Book类

package com.atguigu.spring5.collectiontype;import java.util.List;public class Book {private List<String> list;public void setList(List<String> list) {this.list = list;}public void test() {System.out.println(list);}
}
<?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.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"><!--1 提取list集合类型属性注入--><util:list id="bookList"><value>易筋经</value><value>九阴真经</value><value>九阳神功</value></util:list><!--2 提取list集合类型属性注入使用--><bean id="book" class="com.atguigu.spring5.collectiontype.Book"><property name="list" ref="bookList"></property></bean>
</beans>
@Test
public void testCollection2() {ApplicationContext context =new ClassPathXmlApplicationContext("bean6.xml");Book book1 = context.getBean("book", Book.class);Book book2 = context.getBean("book", Book.class);book1.test();book2.test();System.out.println(book1);System.out.println(book2);
}
http://www.15wanjia.com/news/26749.html

相关文章:

  • 商贸网站建设怎么把产品推广到各大平台
  • 哪有免费的简历模板杭州优化商务服务公司
  • 廊坊中小企业网站制作上海网站seo策划
  • 广州网站建设广州网络推广公司地推平台
  • 做网站最好的公司抚顺网站建设
  • 小米发布会ppt模板整站seo免费咨询
  • 软件技术论坛百度快照优化排名推广怎么做
  • 网站建设补充报价单适合seo的建站系统
  • 网站如何做超级链接百度网盘客服电话
  • 上海 网站建设168推广网
  • 重庆招聘网站哪个好微信朋友圈广告投放收费标准
  • 眉县网站建设天津外贸seo推广
  • 昆明网站开发公司哪家好app推广实名认证接单平台
  • 网站代码跑偏了怎么做推广软文怎么写
  • 雄安做网站价格宁波seo网络推广产品服务
  • 做网站和视频剪辑用曲面屏网站优化提升排名
  • 网站导入wordpress广告推广平台网站
  • 新手如何建立自己网站湖南关键词网络科技有限公司
  • 兴科cms网站建设系统德阳seo
  • 临漳网站建站市场营销产品推广策划方案
  • 服装设计素材网站网络推广销售是做什么的
  • dede网站幻灯片推广普通话宣传周
  • wordpress媒体ip地址关键词排名优化公司地址
  • 手机版制作app软件久久seo综合查询
  • 网站关键词排名系统企业查询网站
  • 建设网站的书籍网站优化价格
  • 门户网站设计说明今日新闻头条大事
  • 福州网站建设方案服务重庆森林百度网盘
  • 深圳哪里有做网站推广的四川省最新疫情情况
  • 做网站用python好还是PHP好seo推广排名平台有哪些