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

北京网站优化步网站建设常用的方法

北京网站优化步,网站建设常用的方法,网站后台管理系统是用什么做的,新共享项目加盟代理学习过 IoC 后,就知道我们可以将对象交给 Spring 进行管理,但是我们在一个类会有若干属性,也就是这个类依赖于这若干个属性,那么我们就可以将交给 Spring 管理的对象注入到这个类中,这也就是依赖注入。 依赖注入有三种…

学习过 IoC 后,就知道我们可以将对象交给 Spring 进行管理,但是我们在一个类会有若干属性,也就是这个类依赖于这若干个属性,那么我们就可以将交给 Spring 管理的对象注入到这个类中,这也就是依赖注入。

依赖注入有三种方式,分别为属性注入、构造方法注入、setter方法注入。下面一一介绍。

一、属性注入

@Autowired

有如下代码:

@Controller
public class TestController {//属性注入@Autowiredprivate TestService service;public void print() {System.out.println("controller");service.print();}
}@Service
public class TestService {public void print() {System.out.println("service");}
}//SpringBoot 启动类
@SpringBootApplication
public class SpringBootDemo2025417Application {public static void main(String[] args) {ApplicationContext context = SpringApplication.run(SpringBootDemo2025417Application.class, args);//获取 TestController 对象TestController controller = context.getBean(TestController.class);controller.print();}}

在 TestController 类中有属性 service,我们通过 @Autowired 注解将 TestService 输入到属性 service 中,这样我们就可以使用 TestService 中的相关方法了,代码运行结果如下:

代码先执行了 TestController 的print 方法,然后又执行了 TestService 的 print 方法。

 现有下面两种情况,一种没加 @Service,另一种是没加 @Autowired,下面我们来分别看看这两种情况。

没加 @Service

运行代码后,发现报错了,错误代码如下:

Description:Field service in com.gjm.demo.controller.TestController required a bean of type 'com.gjm.demo.service.TestService' that could not be found.

由于我们没有加 @Service,就代表我们没有将 TestService 类交给 Spring 进行管理,那么我们在使用 @Autowired 在 Spring 容器中获取对象时就找不到这个对象,就会报这个错。

没加 @Autowired

运行代码后,发现报错了,错误代码如下:

controller
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.gjm.demo.service.TestService.print()" because "this.service" is nullat com.gjm.demo.controller.TestController.print(TestController.java:16)at com.gjm.demo.SpringBootDemo2025417Application.main(SpringBootDemo2025417Application.java:19)

我们发现了,这里报了一个空指针异常,这时为什么呢?

原因是虽然我们加了 @Service 注解,将 TestService 交给 Spring 进行管理,但是由于我们没有加 @Autowired 注解,就使得我们并没有将 TestService 的对象注入给 service,也即是并没有初始化 service,当我们在吊桶TestService 的 print 方法时,就会报空指针异常。这也就是 “controller” 可以显示出来,但 “service” 显示不出来的原因。

二、构造方法注入

顾名思义,就是将属性放到构造方法中在进行注入,代码如下:

@Controller
public class TestController {private TestService service;//构造方法public TestController(TestService service) {this.service = service;}public void print() {System.out.println("controller");service.print();}
}@Service
public class TestService {public void print() {System.out.println("service");}
}@SpringBootApplication
public class SpringBootDemo2025417Application {public static void main(String[] args) {ApplicationContext context = SpringApplication.run(SpringBootDemo2025417Application.class, args);//获取 TestController 对象TestController controller = context.getBean(TestController.class);controller.print();}}

代码运行结果如下:

在上面的构造方法中没有加任何注解,就将 TestService 注入给了 TestController。

但在日常开发中,加上了有参的构造函数,无参的构造函数就默认没有了,为了避免出现问题,我们还会将无参的构造函数也加上,代码如下:

@Controller
public class TestController {private TestService service;//无参构造函数public TestController() {}//有参构造函数public TestController(TestService service) {this.service = service;}public void print() {System.out.println("controller");service.print();}
}

 代码运行结果如下:

controller
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.gjm.demo.service.TestService.print()" because "this.service" is nullat com.gjm.demo.controller.TestController.print(TestController.java:27)at com.gjm.demo.SpringBootDemo2025417Application.main(SpringBootDemo2025417Application.java:19)

又报错了, 是空指针异常。这是因为我们加上了无参的构造函数,在进行构造方法注入的时候,默认的构造方法为无参的构造方法,若没有无参的给构造方法就会使用我们自己写的构造方法。这里我们把无参的构造方法加上了,就会使用无参的构造方法,这时就不会给 service 进行注入,也就是 service 没有斤西瓜初始化,在调用 print 方法时就会出现空指针异常。

上面时只有一个属性的情况,那如果 testController 中有多个属性呢?下面我们介绍有两个属性的情况。代码如下:

package com.gjm.demo.controller;import com.gjm.demo.service.TestService1;
import com.gjm.demo.service.TestService2;
import org.springframework.stereotype.Controller;@Controller
public class TestController {private TestService1 service1;private TestService2 service2;public TestController() {}public TestController(TestService1 service1) {this.service1 = service1;}public TestController(TestService2 service2) {this.service2 = service2;}public TestController(TestService1 service1, TestService2 service2) {this.service1 = service1;this.service2 = service2;}public void print() {System.out.println("controller");service1.print();service2.print();}
}@Service
public class TestService1 {public void print() {System.out.println("service1");}
}@Service
public class TestService2 {public void print() {System.out.println("service2");}
}@SpringBootApplication
public class SpringBootDemo2025417Application {public static void main(String[] args) {ApplicationContext context = SpringApplication.run(SpringBootDemo2025417Application.class, args);//获取 TestController 对象TestController controller = context.getBean(TestController.class);controller.print();}}

在 TestController 中有两个属性,分别为 TestService1 和 TestService2,代码运行结果如下:

controller
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.gjm.demo.service.TestService1.print()" because "this.service1" is nullat com.gjm.demo.controller.TestController.print(TestController.java:49)at com.gjm.demo.SpringBootDemo2025417Application.main(SpringBootDemo2025417Application.java:17)

发现这个报错信息与前一个报错信息是一样的,也是空指针异常。这是因为无论有几个属性,构造方法注入的默认构造方法都是无参构造方法,就相当于 service1 和 service2 书香都没有初始化,在调用 print 方法就会报空指针异常。

那如果将无参的构造方法去掉呢?代码如下:

package com.gjm.demo.controller;import com.gjm.demo.service.TestService1;
import com.gjm.demo.service.TestService2;
import org.springframework.stereotype.Controller;@Controller
public class TestController {private TestService1 service1;private TestService2 service2;public TestController(TestService1 service1) {this.service1 = service1;}public TestController(TestService2 service2) {this.service2 = service2;}public TestController(TestService1 service1, TestService2 service2) {this.service1 = service1;this.service2 = service2;}public void print() {System.out.println("controller");service1.print();service2.print();}
}

代码运行结果如下:

controller
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.gjm.demo.service.TestService1.print()" because "this.service1" is nullat com.gjm.demo.controller.TestController.print(TestController.java:49)at com.gjm.demo.SpringBootDemo2025417Application.main(SpringBootDemo2025417Application.java:17)

同样的,也是空指针异常,那应该怎么办呢?

在构造方法注入中,也可以使用 @Autowired 注解,当在构造方法上使用该注解时,表示的含义就是修改默认的构造函数,于是,可以在全参的构造函数上加上 @Autowired 注解,代码如下:

package com.gjm.demo.controller;import com.gjm.demo.service.TestService1;
import com.gjm.demo.service.TestService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;@Controller
public class TestController {private TestService1 service1;private TestService2 service2;public TestController() {}public TestController(TestService1 service1) {this.service1 = service1;}public TestController(TestService2 service2) {this.service2 = service2;}@Autowiredpublic TestController(TestService1 service1, TestService2 service2) {this.service1 = service1;this.service2 = service2;}public void print() {System.out.println("controller");service1.print();service2.print();}
}

运行结果如下:

这次就运行成功了。

 三、setter 方法注入

setter 方法注入即使用 setter方法进行属性注入,在使用时需要在对应的 setter 方法上加上 @Autowired 注解,如果不加,就会报错。代码如下:

package com.gjm.demo.controller;import com.gjm.demo.service.TestService1;
import com.gjm.demo.service.TestService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;@Controller
public class TestController {private TestService1 service1;private TestService2 service2;@Autowiredpublic void setService1(TestService1 service1) {this.service1 = service1;}@Autowiredpublic void setService2(TestService2 service2) {this.service2 = service2;}public void print() {System.out.println("controller");service1.print();service2.print();}
}@Service
public class TestService1 {public void print() {System.out.println("service1");}
}@Service
public class TestService2 {public void print() {System.out.println("service2");}
}@SpringBootApplication
public class SpringBootDemo2025417Application {public static void main(String[] args) {ApplicationContext context = SpringApplication.run(SpringBootDemo2025417Application.class, args);//获取 TestController 对象TestController controller = context.getBean(TestController.class);controller.print();}}

运行结果如下:

 

http://www.dtcms.com/wzjs/802121.html

相关文章:

  • 开发建设信息的网站网站页面安全监测建设方案
  • 做网站前期需求分析收费么我的网址
  • 长沙专业建设网站企业设置WordPress注册
  • 大厂县建设局网站百度广告一天多少钱
  • 建设网站用什么空间服务器网站qq访客记录原理
  • dw做的网站怎样做成手机版的长春公司推广网站
  • 北京住房和城乡建设部网站官网做网站下导航字号为多大
  • 浙江省建设培训中心的网站有别墅的件怎么写者
  • 网站技术方案怎么写移动端cpu性能天梯图2023
  • 怎么用phpstudy做网站app怎么制作多少钱
  • 优化网站方法网站app开发平台
  • 苏州知名网站制作开发上海买二手车注册公司
  • 创同盟做网站好不好wordpress标签文章数量
  • html静态网站开发专业的个人网站建设哪家便宜
  • 网站服务器租用开票应该百度拍照搜题
  • 成都企业建站模板销售策划
  • 贵阳网站开发制作公司网站的营销推广方案
  • 网站怎么做充值系统下载东莞网站建设模具
  • 企业网站的建设与流程青岛贸易公司 网站制作
  • 有关学风建设网站小红书广告投放平台
  • 广东建设工程信息网站深圳建筑设计院排名
  • 正能量不良网站直接进入免费无锡网站建设方案优化
  • 网站安全建设情况报告WordPress生成海报插件
  • seo网站营销建设网站的目的和功能定位
  • 网站建设会碰到什么问题地方门户网站还能做吗
  • 修改dns连接外国网站个人网站 虚拟主机价格
  • 汕头在线制作网站常州个人做网站
  • 佛山免费建站平台做外贸用哪些网站
  • 宁德网站开发俄文网站策划
  • 网站建设后台和前端淘宝seo搜索优化