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

北京网站建设取名字大全免费查询

北京网站建设,取名字大全免费查询,seo网站排名优化软件是什么,网站备案账号是什么情况个人主页: 几分醉意的CSDN博客_传送门 本文目录💖基于注解的DI✨概念✨Component注解创建对象✨声明组件扫描器✨创建对象的四个注解✨扫描多个包的三种方式✨Value简单类型属性赋值✨Value使用外部属性配置文件✨Autowired引用类型属性赋值&#x1f4ab…

在这里插入图片描述

个人主页: 几分醉意的CSDN博客_传送门

本文目录

  • 💖基于注解的DI
    • ✨概念
    • ✨@Component注解创建对象
    • ✨声明组件扫描器
    • ✨创建对象的四个注解
    • ✨扫描多个包的三种方式
    • ✨@Value简单类型属性赋值
    • ✨@Value使用外部属性配置文件
    • ✨@Autowired引用类型属性赋值
      • 💫byType自动注入
      • 💫byName自动注入
      • 💫required属性
    • ✨@Resource引用类型属性赋值
  • 💖投票传送门(欢迎伙伴们投票)

💖基于注解的DI

✨概念

基于注解的DI:使用spring提供的注解,完成java对象创建,属性赋值。

注解使用的核心步骤:
1.在源代码加入注解,例如@Component。
2.在spring的配置文件,加入组件扫描器的标签。

✨@Component注解创建对象

@Component: 表示创建对象,对象放到容器中。 作用是

  •   属性:value ,表示对象名称,也就是bean的id属性值
    
  •   位置:在类的上面,表示创建此类的对象。
    
 @Component(value = "myStudent") 等同于
< bean id="myStudent" class="com.ba01.Student" />
//使用value 指定对象的名称
//@Component(value = "myStudent")
//省略value
@Component("myStudent")//没有提供自定义对象名称, 使用框架的默认对象名称:类名首字母小写
//@Component
public class Student {private String name;private int age;public Student() {System.out.println("Student无参数构造方法");}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}
}

✨声明组件扫描器

声明组件扫描器:使用注解必须加入这个语句。

<context:component-scan base-package="注解所在的包名"/>
    component-scan:翻译过来是组件扫描器,组件是java对象。属性: base-package 注解在你的项目中的包名。框架会扫描这个包和子包中的所有类,找类中的所有注解。遇到注解后,按照注解表示的功能,去创建对象, 给属性赋值。

认识了组件扫描器,然后我们来到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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><!--声明组件扫描器:使用注解必须加入这个语句component-scan:翻译过来是组件扫描器,组件是java对象。属性: base-package 注解在你的项目中的包名。框架会扫描这个包和子包中的所有类,找类中的所有注解。遇到注解后,按照注解表示的功能,去创建对象, 给属性赋值。--><context:component-scan base-package="com.b01"/></beans>

注意:最上面的都是一些需要的约束文件,当你把组件扫描器写上去后,也会自动的添加对应的约束文件。

那么接下来我们创建一个测试类,进行测试。

public class MyTest {@Testpublic void test01(){String config="applicationContext.xml";ApplicationContext ctx = new ClassPathXmlApplicationContext(config);//后面的myStudent是上面@Component注解创建对象时设置的对象名Student student = (Student) ctx.getBean("myStudent");System.out.println("student=="+student);}
}

✨创建对象的四个注解

经过刚刚的学习,我们已经了解了@Component注解创建对象的使用方法,那么还有其它的相关注解吗?当然有,下面我们将介绍的是和@Component功能相同的创建对象的注解。

1. @Repository : 放在dao接口的实现类上面,表示创建dao对象,持久层对象,能访问数据库。

2).@Service : 放在业务层接口的实现类上面, 表示创建业务层对象, 业务层对象有事务的功能。

3.@Controller:放在控制器类的上面,表示创建控制器对象。 属于表示层对象。控制器对象能接受请求,把请求的处理结果显示给用户。

以上四个注解都能创建对象,但是@Repository @Service @Controller有角色说明, 表示对象是分层的。

✨扫描多个包的三种方式

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><!--声明组件扫描器:使用注解必须加入这个语句component-scan:翻译过来是组件扫描器,组件是java对象。属性: base-package 注解在你的项目中的包名。框架会扫描这个包和子包中的所有类,找类中的所有注解。遇到注解后,按照注解表示的功能,去创建对象, 给属性赋值。--><context:component-scan base-package="com.b01"/><!--扫描多个包的三种方式--><!--第一种,使用多次组件扫描器--><context:component-scan base-package="com.b01"/><context:component-scan base-package="com.b02"/><!--第二种,使用分隔符( ;或,),指定多个包--><context:component-scan base-package="com.b01;com.b02"/><!--第三种:指定父包--><context:component-scan base-package="com"/>
</beans>

✨@Value简单类型属性赋值

@Value: 简单类型属性赋值
属性:value 简单类型属性值
位置:1.在属性定义的上面 ,无需set方法,推荐使用。2.在set方法的上面。

在属性定义的上面定义

package com.bjpowernode.ba02;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component("myStudent")
public class Student {@Value(value = "李四")private String name ;//括号里面的value也可以省略@Value("20")private int age;public Student() {System.out.println("Student无参数构造方法");}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}
}

第二种方式:在set方法的上面

package com.bjpowernode.ba02;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component("myStudent")
public class Student {private String name ;private int age;public Student() {System.out.println("Student无参数构造方法");}@Value("22")public void setAge(int age) {System.out.println("setAge===="+age);this.age = age;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}
}

✨@Value使用外部属性配置文件

这里我创建了一个名为myconf.properties的配置文件

在这里插入图片描述

然后在配置文件输入相应的内容。
在这里插入图片描述
然后我们打开spring配置文件,在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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><!--声明组件扫描器:使用注解必须加入这个语句component-scan:翻译过来是组件扫描器,组件是java对象。属性: base-package 注解在你的项目中的包名。框架会扫描这个包和子包中的所有类,找类中的所有注解。遇到注解后,按照注解表示的功能,去创建对象, 给属性赋值。--><context:component-scan base-package="com.b01"/><!--读取外部的属性配置文件property-placeholder:读取properties这样的文件--><context:property-placeholder location="classpath:/myconf.properties" />
</beans>

注意:location=“classpath:/类路径”

下一步我们开始还有外部文配置件。

语法 :@Value(${"key"})

@Component("myStudent")
public class Student {//使用外部属性文件中的数据,语法 @Value(${"key"})@Value("${myname}")private String name ;private int age;public Student() {System.out.println("Student无参数构造方法");}public void setName(String name) {this.name = name;}//使用外部属性文件中的数据@Value("${myage}")public void setAge(int age) {System.out.println("setAge===="+age);this.age = age;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}
}

✨@Autowired引用类型属性赋值

 * @Autowired: spring框架提供的,给引用类型赋值的,使用自动注入原理。*             支持byName,byType。默认是byType.

💫byType自动注入

  •   位置:1)在属性定义的上面,无需set方法,推荐使用
    
  •       2)在set方法的上面
    

创建一个School类

@Component("mySchool")
public class School {@Value("安徽大学")private String name;@Value("安徽的合肥市")private String address;public void setName(String name) {this.name = name;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "School{" +"name='" + name + '\'' +", address='" + address + '\'' +'}';}
}

在Student类中使用School

@Component("myStudent")
public class Student {//默认使用byType@Autowiredprivate School school;public Student() {System.out.println("Student无参数构造方法");}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", school=" + school +'}';}
}

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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.bjpowernode.ba07"/><!--读取外部的属性配置文件property-placeholder:读取properties这样的文件--><context:property-placeholder location="classpath:/myconf.properties" />
</beans>

测试

public class MyTest3 {@Testpublic void test01(){String config="applicationContext.xml";ApplicationContext ctx = new ClassPathXmlApplicationContext(config);Student student = (Student) ctx.getBean("myStudent");System.out.println("student=="+student);}
}

💫byName自动注入

byName自动注入
1.@Autowired:给引用类型赋值。
2.@Qualifer(value=“bean的id”):从容器中找到指定名称的对象,把这个对象赋值给引用类型。

@Component("myStudent")
public class Student {//byName@Autowired@Qualifier("mySchool")private School school;public Student() {System.out.println("Student无参数构造方法");}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", school=" + school +'}';}
}

测试

public class MyTest3 {@Testpublic void test01(){String config="applicationContext.xml";ApplicationContext ctx = new ClassPathXmlApplicationContext(config);Student student = (Student) ctx.getBean("myStudent");System.out.println("student=="+student);}
}

💫required属性

 *        属性:required :boolean类型的属性, 默认true*             true:spring在启动的时候,创建容器对象时候,会检查引用类型是否赋值成功。*                   如果赋值失败, 终止程序执行,并报错。*             false:引用类型赋值失败,程序正常执行,不报错。引用类型的值是null

示例:
在这里插入图片描述

✨@Resource引用类型属性赋值

 * 引用类型* @Resource: 来自jdk中,给引用类型赋值的,支持byName,byType.默认是byName*             spring支持这个注解的使用。*      位置:1)在属性定义的上面,无需set方法, 推荐使用*            2)在set方法的上面**  说明,使用jdk1.8带有@Resource注解, 高于jdk1.8没有这个@Resource,*  需要加入一个依赖。*    <dependency>*       <groupId>javax.annotation</groupId>*       <artifactId>javax.annotation-api</artifactId>*       <version>1.3.2</version>*     </dependency>

💖投票传送门(欢迎伙伴们投票)


文章转载自:

http://o5ELiMfg.xdpjs.cn
http://0cEJbrUG.xdpjs.cn
http://d66OYDuL.xdpjs.cn
http://sf4TtJb6.xdpjs.cn
http://6PXbxPpx.xdpjs.cn
http://hGASYprT.xdpjs.cn
http://T0ZcYuCd.xdpjs.cn
http://ikRKvatQ.xdpjs.cn
http://8ME3k5Aw.xdpjs.cn
http://xC2dnuU3.xdpjs.cn
http://Wx1o7TAn.xdpjs.cn
http://lcAjVQKO.xdpjs.cn
http://q15QAxfd.xdpjs.cn
http://6QtWORQF.xdpjs.cn
http://iQgGg5ox.xdpjs.cn
http://PJft5kkZ.xdpjs.cn
http://l9jXjzHB.xdpjs.cn
http://CAgWD487.xdpjs.cn
http://iGeXdMLa.xdpjs.cn
http://HDVAMlYr.xdpjs.cn
http://KMKlpVG0.xdpjs.cn
http://ZDjO5vA7.xdpjs.cn
http://vwy8rFvv.xdpjs.cn
http://y1CQ1eaZ.xdpjs.cn
http://AuyAKyHU.xdpjs.cn
http://1TEnqytQ.xdpjs.cn
http://JFFeB419.xdpjs.cn
http://fBApKraL.xdpjs.cn
http://BlRdyM07.xdpjs.cn
http://kf9K1Hgb.xdpjs.cn
http://www.dtcms.com/wzjs/751425.html

相关文章:

  • 西宁市城东区住房和建设局网站少儿编程加盟费一般多少钱
  • 电子商务网站的建站流程制作企业网站费用明细
  • WordPress能够做小说网站吗wordpress 引用 格式
  • 北京建设协会网站网页设计大作业
  • 临湘网站wordpress能用代码吗
  • 国外网站设计的网站phpstudy如何建设网站
  • 网站建设教程学校网站设计接单
  • 做视频网站需要什么证件网络推广专员好做吗
  • 建设网站的页面设计苏州网站设计电话
  • 株洲网站排名无锡新吴区住房建设交通局网站
  • 世界杯直播观看网站三大框架网站开发
  • 企业网站的基本类型包括wordpress主题 摄影师
  • 网站建设管理工作经验介绍网站开发专家:php+mysql网站开发技术与典型案例导航
  • 越秀微网站建设婚纱网站策划书模板
  • 用dw制作视频网站开源小程序模板
  • 龙江网站开发网站备案管局审核
  • 上海网站备案查询微信公众号平台入口官网
  • 网站运营主要做什么wordpress print_r
  • 2021不良正能量免费网站网站排名优化软件哪家好
  • 公司网站设计意见收集自己给公司做网站
  • 网站挂标 怎么做阿里巴巴电子商务网站
  • 阿里巴巴网站策划书企业网站建设报告
  • 免费网站设计软件wordpress自动标签
  • 曲沃网站开发网络推广的几种主要方法
  • asp做的手机网站网页设计表单制作代码
  • 制作企业网站怎么报价南昌网站建设推广专家
  • 快速做网站公司哪家专业营销网站建设服务
  • 深圳罗湖做网站的公司哪家好wordpress文章保存图片
  • 网站建设费怎么做账网站 全屏幻灯片
  • 用虚拟机做服务器搭建网站wordpress aws