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

中石化石油工程建设公司官方网站网络推广员是干什么的

中石化石油工程建设公司官方网站,网络推广员是干什么的,做足球预测的网站,企业官方网站认证文章目录基于xml的自动装配①注解②扫描③新建Maven Module④创建Spring配置文件⑤标识组件的常用注解⑥创建组件⑦扫描组件⑧测试⑨组件所对应的bean的id基于注解的自动装配①场景模拟②Autowired注解③Autowired注解其他细节④Autowired工作流程Autowire 注解的原理Qualifier…

文章目录

      • 基于xml的自动装配
        • ①注解
        • ②扫描
        • ③新建Maven Module
        • ④创建Spring配置文件
        • ⑤标识组件的常用注解
        • ⑥创建组件
        • ⑦扫描组件
        • ⑧测试
        • ⑨组件所对应的bean的id
      • 基于注解的自动装配
        • ①场景模拟
        • ②@Autowired注解
        • ③@Autowired注解其他细节
        • ④@Autowired工作流程
        • @Autowire 注解的原理
        • @Qualifier 注解的使用
        • NoSuchBeanDefinationException

基于xml的自动装配

①注解

和 XML 配置文件一样,注解本身并不能执行,注解本身仅仅只是做一个标记,具体的功能是框架检测到注解标记的位置,然后针对这个位置按照注解标记的功能来执行具体操作。

本质上:所有一切的操作都是Java代码来完成的,XML和注解只是告诉框架中的Java代码如何执行。

②扫描

Spring 为了知道程序员在哪些地方标记了什么注解,就需要通过扫描的方式,来进行检测。然后根据注解进行后续操作。

③新建Maven Module

<dependencies><!-- 基于Maven依赖传递性,导入spring-context依赖即可导入当前所需所有jar包 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.1</version></dependency><!-- junit测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency>
</dependencies>

④创建Spring配置文件

在这里插入图片描述

⑤标识组件的常用注解

@Component:将类标识为普通组件

@Controller:将类标识为控制层组件

@Service:将类标识为业务层组件

@Repository:将类标识为持久层组件

问:以上四个注解有什么关系和区别?
在这里插入图片描述

通过查看源码我们得知,@Controller、@Service、@Repository这三个注解只是在@Component注解的基础上起了三个新的名字。

对于Spring使用IOC容器管理这些组件来说没有区别。所以@Controller、@Service、@Repository这三个注解只是给开发人员看的,让我们能够便于分辨组件的作用。

注意:虽然它们本质上一样,但是为了代码的可读性,为了程序结构严谨我们肯定不能随便胡乱标记。

⑥创建组件

创建控制层组件

@Controller
public class UserController {
}

创建接口UserService

public interface UserService {
}

创建业务层组件UserServiceImpl

@Service
public class UserServiceImpl implements UserService {
}

创建接口UserDao

public interface UserDao {
}

创建持久层组件UserDaoImpl

@Repository
public class UserDaoImpl implements UserDao {
}

⑦扫描组件

情况一:最基本的扫描方式

<context:component-scan base-package="com.atguigu">
</context:component-scan>

情况二:指定要排除的组件(用得多)

<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描组件--><context:component-scan base-package="com.jxz.spring"><!-- context:exclude-filter标签:指定排除规则 --><!--type:设置排除或包含的依据type="annotation",根据注解排除,expression中设置要排除的注解的全类名type="assignable",根据类型排除,expression中设置要排除的类型的全类名-->
<!--        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>--><context:exclude-filter type="assignable" expression="com.jxz.spring.controller.UserController"/></context:component-scan>
</beans>

情况三:仅扫描指定组件

<context:component-scan base-package="com.atguigu" use-default-filters="false"><!-- context:include-filter标签:指定在原有扫描规则的基础上追加的规则 --><!-- use-default-filters属性:取值false表示关闭默认扫描规则 --><!-- 此时必须设置use-default-filters="false",因为默认规则即扫描指定包下所有类 --><!--type:设置排除或包含的依据type="annotation",根据注解排除,expression中设置要排除的注解的全类名type="assignable",根据类型排除,expression中设置要排除的类型的全类名--><context:include-filter type="annotation"expression="org.springframework.stereotype.Controller"/><!--<context:include-filter type="assignable"expression="com.atguigu.controller.UserController"/>-->
</context:component-scan>

⑧测试

@Test
public void testAutowireByAnnotation(){ApplicationContext ac = newClassPathXmlApplicationContext("applicationContext.xml");UserController userController = ac.getBean(UserController.class);System.out.println(userController);UserService userService = ac.getBean(UserService.class);System.out.println(userService);UserDao userDao = ac.getBean(UserDao.class);System.out.println(userDao);
}

⑨组件所对应的bean的id

在我们使用XML方式管理bean的时候,每个bean都有一个唯一标识,便于在其他地方引用。现在使用注解后,每个组件仍然应该有一个唯一标识。

默认情况:

类名首字母小写就是bean的id。例如:UserController类对应的bean的id就是userController。

自定义bean的id:

可通过标识组件的注解的value属性设置自定义的bean的id

@Service(“userService”)//默认为userServiceImpl

public class UserServiceImpl implementsUserService {}

基于注解的自动装配

①场景模拟

参考基于xml的自动装配

在UserController中声明UserService对象

在UserServiceImpl中声明UserDao对象

②@Autowired注解

**在成员变量上直接标记@Autowired注解即可完成自动装配,不需要提供setXxx()方法。**以后我们在项目中的正式用法就是这样。

@Controller
public class UserController {@Autowiredprivate UserService userService;public void saveUser(){userService.saveUser();}
}
public interface UserService {void saveUser();
}
@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserDao userDao;@Overridepublic void saveUser() {userDao.saveUser();}
}
public interface UserDao {void saveUser();
}	
@Repository
public class UserDaoImpl implements UserDao {@Overridepublic void saveUser() {System.out.println("保存成功");}
}

③@Autowired注解其他细节

@Autowired注解还可以标记在构造器和set方法上,完成自动装配

@Controller
public class UserController {private UserService userService;@Autowiredpublic UserController(UserService userService){this.userService = userService;}public void saveUser(){userService.saveUser();}
}
@Controller
public class UserController {private UserService userService;@Autowiredpublic void setUserService(UserService userService){this.userService = userService;}public void saveUser(){userService.saveUser();}
}

④@Autowired工作流程

在这里插入图片描述

@Autowire 注解的原理

a) 默认通过 byType 的方式,在IOC容器中通过类型匹配某个bean为属性赋值,如果不存在类型匹配的话直接报NoSuchBeanDefinationException

b) 当有多个 bean 的类型能匹配到,其会转换为 byName 的方式,根据@Autowired标记位置成员变量的变量名作为bean的id进行匹配。

c) byType和byName都失效的时候,即byType有多个 bean 的类型能匹配到,但byName和其中任何一个类型相同的比较id都不一样,则报 noUniqueBeanDefinationException.

d) 要解决c),可以使用@Qualifier注解:根据@Qualifier注解中指定的名称作为bean的id进行匹配

当执行:

@Test
public void test3(){ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc-annotation.xml");UserController userController = ioc.getBean(UserController.class);userController.saveUser();
}

其中userService成员变量进行扫描的时候

@Autowired
private UserService userService;

会找到两个bean,类别都为UserService,同时默认byName要找的是userService,配置的bean的id却为userServiceAAA,也不等于userService,因此报错:

<context:component-scan base-package="com.jxz.spring"></context:component-scan>
<bean id="userServiceAAA" class="com.jxz.spring.service.impl.UserServiceImpl"></bean>

@Qualifier 注解的使用

我们可以在指定的地方使用下面的注解,强行指定要匹配的id:

package com.jxz.spring.controller;import com.jxz.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;@Controller("jxzController")
public class UserController {@Autowired@Qualifier("userServiceAAA")private UserService userService;public void saveUser(){userService.saveUser();}
}

NoSuchBeanDefinationException

当匹配不到相同类型的Bean完成自动装配的时候,会报 NoSuchBeanDefinationException 的错误,这是因为@Autowire(required = true),必须完成自动装配,不然直接报错。

当修改为 @Autowire(required = false) 的时候,表示不是必须自动状态,找不到Bean则使用默认值,和之前使用XML自动装配的时候的情况一样。

比如:将依赖的UserDaoImpl注释掉,报NoSuchBeanDefinationException

package com.jxz.spring.dao.impl;import com.jxz.spring.dao.UserDao;
import org.springframework.stereotype.Repository;//@Repository
public class UserDaoImpl implements UserDao {@Overridepublic void saveUser() {System.out.println("保存成功");}
}

在对应的调用方修改:

@Service
public class UserServiceImpl implements UserService {@Autowired(required = false)private UserDao userDao;public void saveUser(){userDao.saveUser();}
}

因为要用到private UserDao userDao,但是却不能完成自动装配,因此使用默认值null,于是 userDao.saveUser();报java.lang.NullPointerException


文章转载自:
http://dinncocollenchyma.wbqt.cn
http://dinncoshippen.wbqt.cn
http://dinncomohel.wbqt.cn
http://dinncomedially.wbqt.cn
http://dinncosalivous.wbqt.cn
http://dinncoardeid.wbqt.cn
http://dinncoconsonant.wbqt.cn
http://dinncodorsad.wbqt.cn
http://dinncojalor.wbqt.cn
http://dinncothorntree.wbqt.cn
http://dinncochiaroscuro.wbqt.cn
http://dinncogastrotrich.wbqt.cn
http://dinncobetween.wbqt.cn
http://dinncotreponematosis.wbqt.cn
http://dinncodiscontent.wbqt.cn
http://dinncoairdash.wbqt.cn
http://dinncohotch.wbqt.cn
http://dinncosanbenito.wbqt.cn
http://dinncoantirachitic.wbqt.cn
http://dinncooxfly.wbqt.cn
http://dinncoconciliarism.wbqt.cn
http://dinncorectification.wbqt.cn
http://dinncopaye.wbqt.cn
http://dinncocacomistle.wbqt.cn
http://dinncoemersed.wbqt.cn
http://dinncounfastidious.wbqt.cn
http://dinncobiochemist.wbqt.cn
http://dinncoverbosely.wbqt.cn
http://dinncorhumbatron.wbqt.cn
http://dinncoincise.wbqt.cn
http://dinncosterling.wbqt.cn
http://dinncoacrodont.wbqt.cn
http://dinncodiplomapiece.wbqt.cn
http://dinncolamented.wbqt.cn
http://dinncocogitate.wbqt.cn
http://dinncohouseholder.wbqt.cn
http://dinncobergsonian.wbqt.cn
http://dinncoselectman.wbqt.cn
http://dinncodardanelles.wbqt.cn
http://dinncoerse.wbqt.cn
http://dinncoicositetrahedron.wbqt.cn
http://dinncoreentry.wbqt.cn
http://dinncotrophoblast.wbqt.cn
http://dinncoprecipitancy.wbqt.cn
http://dinncovoyeurist.wbqt.cn
http://dinncoaquarist.wbqt.cn
http://dinncobizonia.wbqt.cn
http://dinncowayward.wbqt.cn
http://dinncowoody.wbqt.cn
http://dinncodimethyltryptamine.wbqt.cn
http://dinncoannularity.wbqt.cn
http://dinncoenlargement.wbqt.cn
http://dinncoboxer.wbqt.cn
http://dinncojingo.wbqt.cn
http://dinncojvc.wbqt.cn
http://dinncoacataleptic.wbqt.cn
http://dinncominutia.wbqt.cn
http://dinncomysophobia.wbqt.cn
http://dinncosemiparasite.wbqt.cn
http://dinncoaccusation.wbqt.cn
http://dinncomousaka.wbqt.cn
http://dinncoenterate.wbqt.cn
http://dinncohibernal.wbqt.cn
http://dinncosphygmomanometer.wbqt.cn
http://dinncoblastosphere.wbqt.cn
http://dinncocoestablishment.wbqt.cn
http://dinncoosage.wbqt.cn
http://dinncolipper.wbqt.cn
http://dinncocinerary.wbqt.cn
http://dinncoodyssean.wbqt.cn
http://dinncowiser.wbqt.cn
http://dinncopinery.wbqt.cn
http://dinncomegadontia.wbqt.cn
http://dinncootp.wbqt.cn
http://dinncoanemometry.wbqt.cn
http://dinncosuppressive.wbqt.cn
http://dinncoshunga.wbqt.cn
http://dinncoshutter.wbqt.cn
http://dinncomotive.wbqt.cn
http://dinncohumouristic.wbqt.cn
http://dinncocoercive.wbqt.cn
http://dinncotwiformed.wbqt.cn
http://dinncooverexert.wbqt.cn
http://dinncodownline.wbqt.cn
http://dinncoglucosuria.wbqt.cn
http://dinncoderange.wbqt.cn
http://dinncopivotal.wbqt.cn
http://dinncoshowbread.wbqt.cn
http://dinncopoach.wbqt.cn
http://dinncoballistocardiogram.wbqt.cn
http://dinnconothingness.wbqt.cn
http://dinncoimaginable.wbqt.cn
http://dinncoectochondral.wbqt.cn
http://dinncononenforceable.wbqt.cn
http://dinncobrassart.wbqt.cn
http://dinncoyaounde.wbqt.cn
http://dinncoknobbiness.wbqt.cn
http://dinncorelative.wbqt.cn
http://dinncoretitrate.wbqt.cn
http://dinncomarksman.wbqt.cn
http://www.dinnco.com/news/125146.html

相关文章:

  • 南京公司网站建立自己如何注册一个网站
  • 谁做政府网站群内网搜索大全引擎
  • 免费个人网站建站申请流程如何分步骤开展seo工作
  • 公司网站怎么做简介seo研究中心vip课程
  • 自动发卡网站开发流量平台有哪些
  • 免费分销平台有哪些重庆seo顾问
  • 专业建设 教学成果奖网站产品设计
  • asp.net做网站有何意义百度网站推广申请
  • 株洲市荷塘区城乡建设局网站长春网站快速排名提升
  • 响应式网站建设流程小辉seo
  • 珠海做网站方案成都百度关键词排名
  • 深圳网站建设民治大道网站建设优化收费
  • seo的收费标准沈阳seo网站推广
  • 如何做网站赚钱6微信小程序免费制作平台
  • 有什么平面设计的网站聊城seo整站优化报价
  • 榆林华科网站建设精准信息300099
  • 做生存分析的网站有哪些合肥百度网站排名优化
  • 做百度糯米网站的团队长沙网站推广公司
  • 武汉市建设学校官方网站百度打广告多少钱
  • 温州做网站哈尔滨seo推广
  • wordpress文章列表获取文章摘要seo网站优化平台
  • wordpress新建的页面长治seo顾问
  • 三合一网站管理系统上饶seo博客
  • 北京网站外包公司谷歌浏览器网页版入口在哪里
  • 顺德公司网站制作搜索引擎算法
  • tk网站免费百度竞价平台官网
  • b2c网站建设 杭州懂得网站推广
  • 网站按钮确定后图片怎么做美国最新新闻头条
  • 营销型网站建设培训刷钻业务推广网站
  • 做民族网站的配色哪些颜色适合百度网盘app官网下载