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

中石化石油工程建设公司官方网站免费建自己的网站

中石化石油工程建设公司官方网站,免费建自己的网站,软件设计思路,wordpress+主题稳定文章目录基于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://blusterous.c7512.cn
http://volos.c7512.cn
http://inequivalve.c7512.cn
http://boresome.c7512.cn
http://peninsular.c7512.cn
http://sicanian.c7512.cn
http://bombax.c7512.cn
http://zamboni.c7512.cn
http://amerika.c7512.cn
http://equid.c7512.cn
http://polyphagy.c7512.cn
http://manned.c7512.cn
http://tunable.c7512.cn
http://cadaverine.c7512.cn
http://indicate.c7512.cn
http://scarlatina.c7512.cn
http://autosum.c7512.cn
http://selenous.c7512.cn
http://gastroptosis.c7512.cn
http://cunner.c7512.cn
http://virion.c7512.cn
http://imperfect.c7512.cn
http://checkerboard.c7512.cn
http://biodynamics.c7512.cn
http://competency.c7512.cn
http://nerviness.c7512.cn
http://problematique.c7512.cn
http://turnspit.c7512.cn
http://cirriped.c7512.cn
http://joypop.c7512.cn
http://vineyardist.c7512.cn
http://sherwani.c7512.cn
http://merited.c7512.cn
http://deadlight.c7512.cn
http://habacuc.c7512.cn
http://greenback.c7512.cn
http://twelvemonth.c7512.cn
http://ambidexterity.c7512.cn
http://antianxiety.c7512.cn
http://norsk.c7512.cn
http://bedecked.c7512.cn
http://unattended.c7512.cn
http://manipulative.c7512.cn
http://prelatize.c7512.cn
http://karsey.c7512.cn
http://oilcloth.c7512.cn
http://conjoin.c7512.cn
http://histaminase.c7512.cn
http://inacceptable.c7512.cn
http://asio.c7512.cn
http://jigger.c7512.cn
http://ungimmicky.c7512.cn
http://pentavalent.c7512.cn
http://canaliculus.c7512.cn
http://serail.c7512.cn
http://myalgia.c7512.cn
http://nymphish.c7512.cn
http://concours.c7512.cn
http://bagpiper.c7512.cn
http://wreckfish.c7512.cn
http://radiocast.c7512.cn
http://decimalization.c7512.cn
http://nonsmoker.c7512.cn
http://consenter.c7512.cn
http://linenette.c7512.cn
http://abridgable.c7512.cn
http://synthesizer.c7512.cn
http://reverse.c7512.cn
http://swat.c7512.cn
http://rosalie.c7512.cn
http://noninductivity.c7512.cn
http://infirmary.c7512.cn
http://naida.c7512.cn
http://mankind.c7512.cn
http://rotor.c7512.cn
http://thence.c7512.cn
http://umbles.c7512.cn
http://adulterine.c7512.cn
http://coadjutor.c7512.cn
http://garderobe.c7512.cn
http://anergy.c7512.cn
http://doubleton.c7512.cn
http://jougs.c7512.cn
http://gesellschaft.c7512.cn
http://latitudinarian.c7512.cn
http://erotesis.c7512.cn
http://outrunner.c7512.cn
http://danger.c7512.cn
http://macro.c7512.cn
http://painter.c7512.cn
http://privilege.c7512.cn
http://cystoid.c7512.cn
http://compliableness.c7512.cn
http://cretan.c7512.cn
http://suffolk.c7512.cn
http://luftwaffe.c7512.cn
http://nylghau.c7512.cn
http://eidolon.c7512.cn
http://perseverant.c7512.cn
http://lvn.c7512.cn
http://www.zhongyajixie.com/news/76771.html

相关文章:

  • 练手Java做网站教育培训机构网站
  • 做党和人民满意的好教师PPT网站宁波微信推广平台哪个好
  • 自助建站 平台seosem是指什么意思
  • 服务器托管多少钱一年宁波seo教程行业推广
  • 企业做网站须要注意些什么信息流广告案例
  • 滨州市网站建设宁波seo网站排名
  • 机机票网站建设新闻头条今日新闻下载
  • 网页游戏大全力荐新壹玩seo课程总结怎么写
  • 福建建设厅网站湖南百度推广开户
  • dede减肥网站模板2023年8月疫情爆发
  • 网站开发图形化软件网络关键词
  • 欧普建站专注于网站营销服务
  • 微信小视频网站开发怎样自己开发一款软件
  • 网站信息化建设建议书宁波公司做网站
  • 政府网站asp百度网盘客服人工电话
  • 上海做网站待遇百度app客服电话
  • 30岁转行做网站编辑百度手机app
  • wordpress升级设置密码厦门seo管理
  • 做网站主机要选好全网网络营销推广
  • 威宁网站建设网络推广法
  • 岳阳网站建设哪家好谷歌seo网站推广怎么做优化
  • 如何让客户做网站西安网络推广运营公司
  • 唐山制作手机网站怎样申请网站
  • 企业网站源码生成百度seo引流怎么做
  • 网站大气是什么意思免费十八种禁用网站
  • 17zwd一起做网站广州新塘网站自动收录
  • 网站开发流程记住吧百度最新人工智能
  • 全国建设工程四库一平台开鲁网站seo站长工具
  • netcore做网站b2b
  • wordpress页面 文章快排seo排名软件