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

怎么去建设微信网站百度推广新手入门

怎么去建设微信网站,百度推广新手入门,网站域名交易,强生公司网站假设一个场景: 一个人(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.tj-hxxt.cn/news/103720.html

相关文章:

  • 网站里自已的微信联系如何做餐饮营销策划方案
  • 佛山微信网站设计视频运营管理平台
  • 做网站 信科网络seo软件优化工具软件
  • 大庆做网站公司浙江百度代理公司
  • wordpress最新版优化广州seo推广营销
  • wordpress恢复旧版宁波seo基础入门
  • 做python题目的网站国际新闻稿件
  • 漯河企业网站建设公司网站查询域名ip
  • 淘客网站怎么做首页足球进球排行榜
  • 阿勒泰地区网站线上推广平台报价
  • 阳江网红打卡地企业网站优化价格
  • 深圳有哪些知名企业深圳优化公司统高粱seo
  • 做分析图超牛的地图网站电商线上推广
  • 做任务刷王者皮肤网站合肥seo排名扣费
  • 做微信推文的网站哪个行业最需要推广
  • 做户外照明有哪些网站肇庆网站建设制作
  • 做跟单员的话应该关注哪些网站自己怎么优化我网站关键词
  • 网站建设证书济南seo培训
  • 建行网站首页登录网上银行网站优化外包找谁
  • 做购物网站安全吗如何有效的推广宣传
  • 如何设计旅游网站seo中文意思
  • 网站排名优化平台seo是什么意思 职业
  • 北京市专业网站制作企业全球疫情最新数据
  • 大淘客网站开发合肥做网站的公司有哪些
  • 机关党建项目网站建设方案google浏览器入口
  • 做期货要看哪几个网站网站搜什么关键词
  • 国外html响应式网站模板下载百度关键词统计
  • 项目管理平台系统廊坊网络推广优化公司
  • 新能源网站开发上海网站优化公司
  • 吉林网站模板软文是什么东西