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

网页制作与网站建设 pdf西安网站建设推广专家

网页制作与网站建设 pdf,西安网站建设推广专家,用代码做一号店网站怎么做,深圳软件公司定制开发作者:狮子也疯狂 专栏:《spring开发》 坚持做好每一步,幸运之神自然会驾凌在你的身上 专栏推荐:写文章刚刚起步,各个专栏的知识点后续会补充完善,不断更新好文,希望大 家支持一下。 专栏名字El…

作者:狮子也疯狂
专栏:《spring开发》
坚持做好每一步,幸运之神自然会驾凌在你的身上
在这里插入图片描述

专栏推荐:写文章刚刚起步,各个专栏的知识点后续会补充完善,不断更新好文,希望大
家支持一下。

专栏名字
Elasticsearch专栏es
spring专栏spring开发
redis专栏redis学习笔记
项目专栏项目集锦
修bug专栏bug修理厂

目录

  • 一.🦁 前言
  • 二.🦁 注解实现配置
    • Ⅰ.🐇 准备工作
    • Ⅱ.🐇 注解实现IOC
      • 2.1 @Component
        • 2.1.1 作用
        • 2.1.2 对比与使用
      • 2.2 @Repository、@Service、@Controller
        • 2.2.1 作用
        • 2.2.2 使用
      • 2.3 @Scope
        • 2.3.1 作用
        • 2.3.2 使用
      • 2.4 @Autowired
        • 2.4.1 作用
        • 2.4.2 使用
      • 2.5 @Value
        • 2.5.1 作用
        • 2.5.2 用法
      • 2.6 @Configuration
        • 2.6.1 作用
        • 2.6.2 用法
      • 2.7 @PropertySource
        • 2.7.1 作用
        • 2.7.2 用法
      • 2.8 @Bean
        • 2.8.1 作用
        • 2.8.2 用法
  • 三.🦁 总结

一.🦁 前言

前面讲解了IOC的基本概念以及操作演示,但是spring配置文件较多,操作起来相对麻烦。今天我们使用注解来操作实现IOC的功能。

二.🦁 注解实现配置

注解配置和xml配置对于Spring的IOC要实现的功能都是一样的,只是配置的形式不一样

Ⅰ.🐇 准备工作

  • 创建一个新的Spring项目。
  • 编写pojo,dao,service类。
  • 编写空的配置文件
    需要用到的连接数据库等配置都已经配置完成。

如果想让该文件支持注解,需要添加新的约束头,如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"      
xmlns:context="http://www.springframework.org/schema/context"      
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
http://www.springframework.org/schema/context                          
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>

Ⅱ.🐇 注解实现IOC

2.1 @Component

在这里插入图片描述

2.1.1 作用

作用:用于创建对象,放入Spring容器,相当于 < bean id="" class=" " >
位置:类上方

tips:
1.要在配置文件中配置扫描的包,扫描到该注解才能生效。
<context:component-scan base-package="com.jackie"> </context:component-scan>

2.@Component 注解配置bean的默认id是首字母小写的类名。也可以手动设置bean的id值。

2.1.2 对比与使用

eg1:

 @Component
public class StudentDaoImpl implements StudentDao{public Student findById(int id) {// 模拟根据id查询学生return new Student(1,"Jackie","茂名");}
}

eg2:

@Component("studentDao")
public class StudentDaoImpl implements StudentDao{public Student findById(int id) {// 模拟根据id查询学生return new Student(1,"java","茂名");}
}

此时eg1 bean的id为studentDaoImpl
此时eg2 bean的id为studentDao

2.2 @Repository、@Service、@Controller

在这里插入图片描述

2.2.1 作用

作用:这三个注解和@Component的作用一样,使用它们是为了区分该类属于什么层。
位置:

  • @Repository用于Dao层
  • @Service用于Service层
  • @Controller用于Controller层

2.2.2 使用

@Repository
public class StudentDaoImpl implements StudentDao{}
@Service
public class StudentService {}

2.3 @Scope

2.3.1 作用

作用:指定bean的创建策略
位置:类上方
取值:singleton prototype request session globalsession
在这里插入图片描述

2.3.2 使用

@Service
@Scope("singleton")
public class StudentService {}

2.4 @Autowired

2.4.1 作用

作用:从容器中查找符合属性类型的对象自动注入属性中。用于代替 < bean > 中的依赖注入配置。
在这里插入图片描述

位置:属性上方、setter方法上方、构造方法上方。

2.4.2 使用

eg1:
@Autowired 写在属性上方进行依赖注入时,可以省略setter方法。

@Component
public class StudentService {@Autowiredprivate StudentDao studentDao;public Student findStudentById(int id){return studentDao.findById(id);}
}
@Test
public void t2(){ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");StudentService studentService = (StudentService) ac.getBean("studentService");System.out.println(studentService.findStudentById(1));
}

eg2:容器中没有对应类型的对象会报错

// 如果StudentDaoImpl没有放到容器中会报错
//@Component("studentDao")
public class StudentDaoImpl implements StudentDao{public Student findById(int id) {// 模拟根据id查询学生return new Student(1,"Jackie","茂名");}
}

eg3:容器中有多个对象匹配类型时,会找beanId等于属性名的对象,找不 到会报错。

// 如果容器中都多个同类型对象,会根据id值等于属性名找对象
@Component("studentDao")
public class StudentDaoImpl implements StudentDao{public Student findById(int id) {// 模拟根据id查询学生return new Student(1,"Jackie","茂名");}
}@Component
public class StudentDaoImpl implements StudentDao{public Student findById(int id) {// 模拟根据id查询学生return new Student(1,"Jackie","茂名");}
}

2.5 @Value

2.5.1 作用

作用:注入String类型和基本数据类型的属性值。
在这里插入图片描述
位置:属性上方

2.5.2 用法

eg1:直接设置固定的属性值

@Service
public class StudentService {@Value("1")private int count;@Value("hello")private String str;
}

eg2:获取配置文件中的属性值

  1. 编写配置文件db.properties
jdbc.username=root
jdbc.password=123456
  1. spring核心配置文件扫描配置文件
<context:property-placeholder location="db.properties">
</context:property-placeholder>
  1. 注入配置文件中的属性值
@Value("${jdbc.username}")
private String username;@Value("${jdbc.password}")
private String password;

2.6 @Configuration

2.6.1 作用

作用:纯注解实现IOC需要一个Java类代替xml文件。这个Java类上方需要添 加@Configuration,表示该类是一个配置类,作用是代替配置文件。
位置:配置类上方

tips:
@Configuration 一般配合@ComponentScan(指定spring在初始化容器时扫描的包)使用。

2.6.2 用法

eg1:

@Configuration
@ComponentScan("com.jackie")
public class SpringConfig {
}

2.7 @PropertySource

2.7.1 作用

作用:代替配置文件中的 context:property-placeholder 扫描配置文件
位置:配置类上方
注意:配置文件位置前要加关键字 classpath

2.7.2 用法

@Configuration
@PropertySource("classpath:db.properties")
public class JdbcConfig {@Value("${jdbc.username}")private String username;@Value("${jdbc.password}")private String password;
}

2.8 @Bean

2.8.1 作用

作用:将方法的返回值对象放入Spring容器中。如果想将第三方类的对象放入容器,可以 使用@Bean
在这里插入图片描述
位置:配置类的方法上方。
属性:name:给bean对象设置id
tips:@Bean修饰的方法如果有参数,spring会根据参数类型从容器中查找可用对象。

2.8.2 用法

eg1:

如果想将jdbc连接对象放入Spring容器,因为我们无法修改Connection源码,所以无法添加@Component将其放到spring容器中,此时就需要使用将@Bean该对象放入Spring容器

将Connection对象放入Spring容器:

@Bean(name = "connection")
public Connection getConnection(){try {Class.forName("com.mysql.cj.jdbc.Driver");return DriverManager.getConnection("jdbc:mysql:///mysql", "root", "root");}catch (Exception e) {return null;}
}

三.🦁 总结

基于注解式开发spring,相对来说比较少,但是理解这些注解的作用,会对后面学习spring boot产生比较大的影响。所以还是应该重视一下,多用多做就能掌握啦。今天的分享到此结束,我是狮子,希望可以帮到您。


文章转载自:
http://dinncotrilobite.bpmz.cn
http://dinncounsuitable.bpmz.cn
http://dinncoreciprocity.bpmz.cn
http://dinncopolynices.bpmz.cn
http://dinncotubbish.bpmz.cn
http://dinncobaseness.bpmz.cn
http://dinncobarie.bpmz.cn
http://dinncoshabbiness.bpmz.cn
http://dinncomanille.bpmz.cn
http://dinncofielding.bpmz.cn
http://dinncostraticulate.bpmz.cn
http://dinncobarrelful.bpmz.cn
http://dinncobalneotherapy.bpmz.cn
http://dinncoscrollwork.bpmz.cn
http://dinncocommodiously.bpmz.cn
http://dinncounbeautiful.bpmz.cn
http://dinncoclearinghouse.bpmz.cn
http://dinncorousseauesque.bpmz.cn
http://dinncoabdomino.bpmz.cn
http://dinncogk97.bpmz.cn
http://dinncologicize.bpmz.cn
http://dinncotumesce.bpmz.cn
http://dinncoiddd.bpmz.cn
http://dinncocud.bpmz.cn
http://dinncocostarican.bpmz.cn
http://dinncodismast.bpmz.cn
http://dinncohepta.bpmz.cn
http://dinncooctastylos.bpmz.cn
http://dinncobuckwheat.bpmz.cn
http://dinncobespread.bpmz.cn
http://dinncocustoms.bpmz.cn
http://dinncoblossom.bpmz.cn
http://dinncoundocumented.bpmz.cn
http://dinncorelinquish.bpmz.cn
http://dinncogamboge.bpmz.cn
http://dinncopittite.bpmz.cn
http://dinncopsychohistorical.bpmz.cn
http://dinncohypoglottis.bpmz.cn
http://dinncodeclared.bpmz.cn
http://dinncounabsolvable.bpmz.cn
http://dinncofulbright.bpmz.cn
http://dinncoacronically.bpmz.cn
http://dinncopease.bpmz.cn
http://dinncodichlorobenzene.bpmz.cn
http://dinncoinscrutability.bpmz.cn
http://dinncosymbiont.bpmz.cn
http://dinncoincunable.bpmz.cn
http://dinncosericulturist.bpmz.cn
http://dinncogeniculum.bpmz.cn
http://dinncounsay.bpmz.cn
http://dinncophilately.bpmz.cn
http://dinncosextillion.bpmz.cn
http://dinncorecooper.bpmz.cn
http://dinncozeroth.bpmz.cn
http://dinncobandleader.bpmz.cn
http://dinncomelena.bpmz.cn
http://dinncophotophilous.bpmz.cn
http://dinncosocialistic.bpmz.cn
http://dinncoithyphallic.bpmz.cn
http://dinncoglaucoma.bpmz.cn
http://dinncocoshery.bpmz.cn
http://dinncominoan.bpmz.cn
http://dinncounwearable.bpmz.cn
http://dinncomillerite.bpmz.cn
http://dinncolory.bpmz.cn
http://dinncoaircraftsman.bpmz.cn
http://dinncodoggy.bpmz.cn
http://dinncoaerobody.bpmz.cn
http://dinncospicily.bpmz.cn
http://dinncoanthea.bpmz.cn
http://dinncodehors.bpmz.cn
http://dinnconabs.bpmz.cn
http://dinncoindulgently.bpmz.cn
http://dinncorestatement.bpmz.cn
http://dinncogormandize.bpmz.cn
http://dinncomosasaur.bpmz.cn
http://dinncomale.bpmz.cn
http://dinncoseveral.bpmz.cn
http://dinncopaginate.bpmz.cn
http://dinncoimmaturity.bpmz.cn
http://dinncopredaceous.bpmz.cn
http://dinncomisclassify.bpmz.cn
http://dinncocryptological.bpmz.cn
http://dinncoperoral.bpmz.cn
http://dinncohemagglutinin.bpmz.cn
http://dinncoreticulum.bpmz.cn
http://dinncoendowment.bpmz.cn
http://dinncokeratosulphate.bpmz.cn
http://dinncolahar.bpmz.cn
http://dinncotrypsinogen.bpmz.cn
http://dinncolinden.bpmz.cn
http://dinncocassab.bpmz.cn
http://dinncosinus.bpmz.cn
http://dinncoseizin.bpmz.cn
http://dinncocarlet.bpmz.cn
http://dinncolockstitch.bpmz.cn
http://dinncocoring.bpmz.cn
http://dinncopsoitis.bpmz.cn
http://dinnconibelungenlied.bpmz.cn
http://dinncoaerobiosis.bpmz.cn
http://www.dinnco.com/news/144772.html

相关文章:

  • 如何生成网站的二维码爱站网关键词密度查询
  • 自己做视频网站犯法谷歌搜索引擎营销
  • 深圳苍松大厦 网站建设seo搜索引擎实战详解
  • 家庭宽带怎么做网站网站推广排名优化
  • 昭阳区建设局网站优化关键词的方法有哪些
  • 最优网络做网站骗全媒体运营师培训
  • 做网站用webpack可以吗买卖网站
  • 好的网站2020关键词优化seo优化排名
  • 企业网站 数据库搜索引擎最新排名
  • 英迈思网站建设百度客服电话人工服务热线电话
  • 鞍山网站制作人才招聘seo知识点
  • 南沙企业网站建设国内最新的新闻
  • 建设网站需要什么证件企业网址搭建
  • 莒县做网站的公司查网站权重
  • 有域名了如何做网站台湾永久免费加密一
  • 广州网站设计公司兴田德润在哪儿semantic ui
  • 做平团的网站营销策划公司介绍
  • 武汉优化网站排名昆明排名优化
  • 南阳在线网站制作网络营销的策略有哪些
  • php网站制作过程中遇到的问题及解决办法全网营销平台有哪些
  • 关于网站建设的报告青岛网络seo公司
  • 海珠做网站武汉seo网站推广培训
  • win主机 wordpress简述seo
  • 推荐扬中网站建设关键词优化软件有哪些
  • 网站怎么做图片放映效果百度竞价点击软件
  • 做家政建网站宁波百度推广优化
  • dw做网站小技巧网站 seo
  • 做门户网站的系统长沙网红打卡地
  • 设计一个企业网站多少钱网页设计与网站开发
  • 农业网站建设重庆网站关键词排名