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

网站海外推广平台十大免费行情软件在线观看

网站海外推广平台,十大免费行情软件在线观看,北京网站建设公司费用浩森宇特,wordpress google seo假设一个场景: 一个人(Person)有一条狗(Dog)和一只猫(Cat),狗和猫都会叫,狗叫是“汪汪”,猫叫是“喵喵”,同时人还有一个自己的名字。 将上述场景 抽象出三个实体类&…

假设一个场景:
一个人(Person)有一条狗(Dog)和一只猫(Cat),狗和猫都会叫,狗叫是“汪汪”,猫叫是“喵喵”,同时人还有一个自己的名字。
将上述场景 抽象出三个实体类:Person、Dog和Cat

Persion.java

package com.zbt.autowire.dto;/*** @author* @createAt 2025/1/9 20:44*/public class Person {private Dog dog;private Cat cat;private String name;public Dog getDog() {return dog;}public void setDog(Dog dog) {this.dog = dog;}public Cat getCat() {return cat;}public void setCat(Cat cat) {this.cat = cat;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Person{" +"dog=" + dog +", cat=" + cat +", name='" + name + '\'' +'}';}
}

Dog.java

package com.zbt.autowire.dto;/*** @author* @createAt 2025/1/9 20:42*/public class Dog {public void shout(){System.err.println("小狗叫:wang~");}
}

Cat.java

package com.zbt.autowire.dto;/*** @author* @createAt 2025/1/9 20:43*/public class Cat {public void shout(){System.err.println("小猫叫:miao~");}
}

手动装配

在配置文件中注入属性—这种方式为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="dog" class="com.zbt.autowire.dto.Dog"/><bean id="cat" class="com.zbt.autowire.dto.Cat"/><bean id="person" class="com.zbt.autowire.dto.Person"><property name="name" value="张三"/><property name="cat" ref="cat"/><property name="dog" ref="dog"/></bean>
</beans>

通过配置文件 自动装配

bean标签有一个属性叫做 autowire 它配置的是自动装配方式

byName注入

通过配置的其他的bean的id自动匹配参数名,相同则注入(换种说法:将和属性名相同的Bean id 对应的对象当做值进行注入)

<?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="dog" class="com.zbt.autowire.dto.Dog"/><bean id="cat" class="com.zbt.autowire.dto.Cat"/><!--<bean id="person" class="com.zbt.autowire.dto.Person"><property name="name" value="张三"/><property name="cat" ref="cat"/><property name="dog" ref="dog"/></bean>--><bean id="person" class="com.zbt.autowire.dto.Person" autowire="byName"><property name="name" value="张三"/></bean>
</beans>

上面的例子中,Person中有cat和dog 在配置文件中Cat和Dog类的Bean id也是cat和dog,此时可以通过在Person的Bean上设置**autowire=“byName”**自动将cat和dog注入到Person中
注意 如果需要被注入的bean的id与属性名不同则无法注入
例如下面这种情况就无法给Person注入dog

<?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="dog1" class="com.zbt.autowire.dto.Dog"/><bean id="cat" class="com.zbt.autowire.dto.Cat"/><!--<bean id="person" class="com.zbt.autowire.dto.Person"><property name="name" value="张三"/><property name="cat" ref="cat"/><property name="dog" ref="dog"/></bean>--><bean id="person" class="com.zbt.autowire.dto.Person" autowire="byName"><property name="name" value="张三"/></bean>
</beans>

接下来我们在另一个包entity下在新建一个Cat类,同样也有一个shout方法

package com.zbt.autowire.entity;/*** @author* @createAt 2025/1/9 21:09*/public class Cat {
public void shout(){System.err.println("entity里的Cat");
}
}

在配置文件中对两个Cat类的bean做如下配置

<?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="dog1" class="com.zbt.autowire.dto.Dog"/><bean id="cat1" class="com.zbt.autowire.dto.Cat"/><bean id="cat" class="com.zbt.autowire.entity.Cat"/><!--<bean id="person" class="com.zbt.autowire.dto.Person"><property name="name" value="张三"/><property name="cat" ref="cat"/><property name="dog" ref="dog"/></bean>--><bean id="person" class="com.zbt.autowire.dto.Person" autowire="byName"><property name="name" value="张三"/></bean>
</beans>

此时Person注入的cat对象为entity包下的Cat

编写如下测试代码可验证结果:

import com.zbt.autowire.dto.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @author* @createAt 2025/1/9 21:01*/public class MyTest {@Testpublic void testAutowire(){ApplicationContext context = new ClassPathXmlApplicationContext("beansAutowire.xml");Person person = context.getBean("person", Person.class);person.getCat().shout();}
}

控制台输出为
在这里插入图片描述
注意我划线部分的报错,意思是无法将entity下的Cat转换为dto下的Cat(因为Person类中的Cat类型为dto下的Cat,而注入的根据bean的id匹配到的是entity下的,所以报了这个错误)

byType注入

通过配置的其他的bean的calss设置的类自动匹配参数类型,相同则注入(换种说法:查找与属性类型相同的Bean对应的对象当做值进行注入)

<?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="dog1" class="com.zbt.autowire.dto.Dog"/><bean id="cat" class="com.zbt.autowire.dto.Cat"/><!--<bean id="person" class="com.zbt.autowire.dto.Person"><property name="name" value="张三"/><property name="cat" ref="cat"/><property name="dog" ref="dog"/></bean>--><bean id="person" class="com.zbt.autowire.dto.Person" autowire="byType"><property name="name" value="张三"/></bean>
</beans>

上面的例子中,Person中有cat和dog 在配置文件中也配置了Cat和Dog类的Bean,此时可以通过在Person的Bean上设置**autowire=“byType”**自动将cat和dog注入到Person中,与Cat和Dog的Bean的id无关。

同时在配置文件中也配置entity.cat的bean

<?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="dog1" class="com.zbt.autowire.dto.Dog"/><bean id="cat" class="com.zbt.autowire.dto.Cat"/><bean id="cat1" class="com.zbt.autowire.entity.Cat"/><!--<bean id="person" class="com.zbt.autowire.dto.Person"><property name="name" value="张三"/><property name="cat" ref="cat"/><property name="dog" ref="dog"/></bean>--><bean id="person" class="com.zbt.autowire.dto.Person" autowire="byType"><property name="name" value="张三"/></bean>
</beans>

发现最终注入的还是dto下的Cat(可以通过上述的测试代码可以发现控制台输出的是:“小猫叫:miao~”而不是:“entity里的Cat”,可以验证结论)

弊端: 必须保证每个类只能在配置文件里配置一次,一个类多次被配置时无法使用byType自动装配

通过注解 自动装配(@Autowire 、@Resource)

需要在配置文件中导入约束(context — 共三个)并添加一项配置( context:annotation-config/) 才能支持注解的使用
context 约束:

  1. xmlns:context=“http://www.springframework.org/schema/context”
    2.xsi:schemaLocation下的:" http://www.springframework.org/schema/context"
    3.xsi:schemaLocation下的:" https://www.springframework.org/schema/context/spring-context.xsd"
<?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:context="http://www.springframework.org/schema/context"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>

@Autowire

原理:

  1. Autowire注解优先通过byType方式匹配bean,byType匹配不到再通过byName匹配
  2. 为了防止Autowire匹配不到bean,经常将Autowire与Qualifire搭配使用 通过 @Qualifire(value=“配置文件中的bean id”) 指定要匹配的bean 的id(Autowire + Qualifire 等价于 Resource)

用法
写在属性名上一行或者set方法上一行即可自动装配
推荐写在属性名上一行,因为属性名上使用此注解后,可以省略set方法(前提是配置文件中配置了对应的bean)
autowire的require属性:默认为true 代表这个属性的值不能为null,如果@Autowire(require = false) 则代表这个属性可以为null

@Resource

@Resource是jdk自带的注解
它可以指定name(bean的id),如@Resource(name=“cat”)
在不指定name的情况下:先byName,后byType。与Autowire的正好相反
用法
写在属性名上一行或者set方法上一行即可自动装配
推荐写在属性名上一行,因为属性名上使用此注解后,可以省略set方法(前提是配置文件中配置了对应的bean)

http://www.yayakq.cn/news/721206/

相关文章:

  • 呼和浩特做网站的地方网站模板打包下载
  • 四川网站建设 旋风天长网站开发
  • 天津企业网站建设价格wordpress英文变成中文
  • 网站开发工资多少钱一个月网站换主推关键词会怎么样
  • 网站图标怎么下载怎样做浏览的网站不被发现
  • 制作网站建网站怎样装修公司网站
  • 网站商城建设6win7 iis默认网站设置
  • 徐州市城乡建设局官方网站模板之家怎么免费下载
  • 磐安做网站南昌中企动力做的网站怎么样
  • 服装设计网站哪个好微信小程序是干什么用的
  • 建设网站公司兴田德润官方地址网站建设优化及推广
  • 烟台网站排名优化公司最新网站建设的模板下载
  • 济南网站建站公司采购管理系统软件
  • 服务器做网站教程电销系统外呼软件
  • 做本地网站要服务器吗wordPress主题模板站
  • 郑州cms建站模板如何做交易网站
  • 厦门专业网站建设建站深圳网页设计培训学校
  • 做机械设计的网站电子商务网站建设前期规划方案
  • 仿皮皮淘网站开发全程培训wordpress缩略图中大大
  • 百度网站制作加盟网站开发费用
  • 电影网站怎么做不犯法做网站之前需要准备什么软件
  • 怎么开始做网站在百度做推广送网站好吗
  • 记事本做的网站链接怎么装饰尧都网站建设
  • 专业网站排名优化提升网站浏览量
  • 青岛网站seo价格南昌比较好的网站设计
  • 企业网站的建立多少钱有关建设旅游网站的公司
  • 做淘宝主要看哪些网站wordpress两个头部
  • asp简单网站开发大作业网站建设方案
  • 如何免费建com的网站安康平台介绍
  • 如果将域名指向网站外贸多语言网站免费源码