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

厦门做网页网站的公司合肥网络优化推广公司

厦门做网页网站的公司,合肥网络优化推广公司,做智能网站软件,企业网站做seo文章目录 1. 实例化Bean2. 使用FactoryBean3. 依赖注入(DI)3.1 AutoWired 属性注入(查找顺序:先类型,后名字)3.2 AutoWired 在构造函数&参数上的使用3.3 Inject和Resource 进行依赖注入3.4 Value 进行注入 1. 实例化Bean 默认使用无参构造函数&…

文章目录

    • 1. 实例化Bean
    • 2. 使用FactoryBean
    • 3. 依赖注入(DI)
      • 3.1 @AutoWired 属性注入(查找顺序:先类型,后名字)
      • 3.2 @AutoWired 在构造函数&参数上的使用
      • 3.3 @Inject和@Resource 进行依赖注入
      • 3.4 @Value 进行注入

1. 实例化Bean

默认使用无参构造函数,如果在这个Bean下定义了一个有参的构造方法(没有写无参构造方法),实例化时使用的是这个有参构造方法;如果有多个有参的构造方法(没有写无参构造方法),此时实例化时会报错,因为不知道使用哪个构造方法。

package com.lize.demo.dao;import com.lize.demo.TestBean;
import com.lize.demo.TestBean2;
import org.springframework.stereotype.Component;@Component("UserDao")
public class UserDao {private TestBean tb;private TestBean2 tb2;
//    public UserDao(){
//        System.out.println("构造函数");
//    }public UserDao(TestBean tb){System.out.println("有参的构造函数"+tb);this.tb = tb;}public UserDao(TestBean tb,TestBean2 tb2){System.out.println("有参的构造函数"+tb+tb2);this.tb = tb;this.tb2 = tb2;}public void printUserDao(){System.out.println("UserDao");}}
package com.lize.demo;import com.lize.demo.dao.UserDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class DemoApplicationTests {@Autowiredprivate UserDao ud;@Testvoid contextLoads() {ud.printUserDao();}}

报错信息如下:
在这里插入图片描述
此时如果要实例化有参的Bean,可以使用注解@Bean的方式来进行,如下:

package com.lize.demo.dao;import com.lize.demo.TestBean;
import com.lize.demo.TestBean2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;public class UserDao {private TestBean tb;private TestBean2 tb2;public UserDao(TestBean tb){System.out.println("有参的构造函数"+tb);this.tb = tb;}public UserDao(TestBean tb,TestBean2 tb2){System.out.println("有参的构造函数"+tb+tb2);this.tb = tb;this.tb2 = tb2;}public void printUserDao(){System.out.println("UserDao");}}
package com.lize.demo.config;import com.lize.demo.TestBean;
import com.lize.demo.TestBean2;
import com.lize.demo.dao.UserDao;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;@Configuration
public class SpringConnfig {@Beanpublic UserDao getUserDao(TestBean tb,TestBean2 tb2){return new UserDao(tb,tb2);}
}

运行结果如下:
在这里插入图片描述

2. 使用FactoryBean

定义一个类,让其实现FactoryBean这个接口,并重写其下方法,如下:

package com.lize.demo.service;import com.lize.demo.TestBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Service;@Service("UserService")
public class UserService implements FactoryBean {@Overridepublic Object getObject() throws Exception {return new TestBean();}@Overridepublic Class<?> getObjectType() {return null;}
}
package com.lize.demo;import com.lize.demo.dao.UserDao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);System.out.println(context.getBean("UserService"));}}

此时的打印结果如下:
在这里插入图片描述
上述打印结果为TestBean,而不是UserService这个Bean。如果要获取UserService这个Bean,可以通过类型获取,如下:
在这里插入图片描述
还有一种做法就是在第一种的基础上,通过字符串获取Bean,字符串前面加上“&”符号,如下:
在这里插入图片描述
如果想通过类型获取TestBean这个Bean,可以在getObjectType方法下添加对应的类型信息,如下:
在这里插入图片描述
在这里插入图片描述
运行结果:
在这里插入图片描述
总结一下:
使用FactoryBean来实例化Bean。

  • FactoryBean是一个接口;
  • 需要有一个Bean,一旦这个Bean实现FactoryBean就成为了特殊的Bean;
  • 需要实现两个方法
    • getObject,当通过Bean实际名获取到的Bean就是getObject返回的对象(伪装);
    • getObjectType,想通过获取对应的类型去获取这个伪装的Bean,就需要返回getObject返回的对象的类型;
  • 可以自由控制Bean的构造方法来实例化Bean

3. 依赖注入(DI)

3.1 @AutoWired 属性注入(查找顺序:先类型,后名字)

使用这个注解,首先会通过类型去容器中查找是否有这个Bean,如果没有,再通过名字去查找是否有这个Bean。

直接在类上添加注解@Component定义Bean,名字为testBean3

package com.lize.demo;import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;@Component
//@Primary
public class TestBean3 {private String name;public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "TestBean3{" +"name='" + name + '\'' +'}';}
}

使用配置类定义Bean,名字为:TestBean31

package com.lize.demo.config;import com.lize.demo.TestBean3;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class SpringConnfig {@Beanpublic TestBean3 TestBean31(){return new TestBean3();}
}

上述定义了两个类型相同的Bean。

package com.lize.demo;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class TestBean4 {@Autowiredprivate TestBean3 testBean3;@Overridepublic String toString() {return "TestBean4{" +"testBean3=" + testBean3 +'}';}
}

在TestBean4 中引入这个Bean,然后在单元测试中输出结果如下:

package com.lize.demo;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class DemoApplicationTests {@Autowiredprivate TestBean4 tb4;@Testvoid contextLoads() {System.out.println(tb4);}
}

运行结果如下:
在这里插入图片描述
可以看到,此时因为有两个Bean类型相同,因此采用名字去查找Bean,在TestBean4中使用的Bean名字为testBean3,因此输出的结果中的Bean为直接在类上添加注解@Component的那个Bean(name的值默认为空)。如果把TestBean4中的那个Bean的名字修改为TestBean31,那么此时的输出结果就是通过配置类定义的那个Bean了。

package com.lize.demo.config;import com.lize.demo.TestBean3;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class SpringConnfig {@Beanpublic TestBean3 TestBean31(){TestBean3 testBean3 = new TestBean3();testBean3.setName("TestBean31");return testBean3;}}
package com.lize.demo;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class TestBean4 {@Autowiredprivate TestBean3 TestBean31;@Overridepublic String toString() {return "TestBean4{" +"testBean3=" + TestBean31 +'}';}
}

在这里插入图片描述

如果通过名字还是查找不到,比如把TestBean4中的引入那个Bean的名字修改为tb3,那么此时就会报错了。
在这里插入图片描述

此时可以在定义Bean的那个类上添加注解 @Primary,表示主要的。
在这里插入图片描述

在这里插入图片描述
另外一种解决方法就是在这个TestBean4引入的那个Bean下指明到底是哪个Bean(使用注解 @Qualifier),如下:
在这里插入图片描述

3.2 @AutoWired 在构造函数&参数上的使用

如果一个Bean定义了多个有参的构造函数,但是没有定义默认的构造函数(无参构造函数),此时在另外一个类中引入这个Bean,然后在单元测试中输出这个Bean,会报错,如下:

package com.lize.demo.dao;import com.lize.demo.TestBean;
import com.lize.demo.TestBean2;
import org.springframework.stereotype.Component;@Component("UserDao")
public class UserDao {private TestBean tb;private TestBean2 tb2;
//    public UserDao(){
//        System.out.println("构造函数");
//    }public UserDao(TestBean tb){System.out.println("有参的构造函数"+tb);this.tb = tb;}public UserDao(TestBean tb,TestBean2 tb2){System.out.println("有参的构造函数"+tb+tb2);this.tb = tb;this.tb2 = tb2;}public void printUserDao(){System.out.println("UserDao");}}

在这里插入图片描述
如果此时想要正常输出,可以在对应的构造函数上面添加注解@AutoWired ,如下,此时正常输出。
在这里插入图片描述
TestBean、TestBean2如下形式:
在这里插入图片描述
如果想要为构造函数中的参数设置为不必须的,需要在参数上面设置 @Autowired(required = false),直接在构造函数上设置是不生效的,因此会报错(下面没有给出),如下:
在这里插入图片描述

在这里插入图片描述
此时打印结果为null。
另外,还可以写在单元测试的方法上面,如下:
在这里插入图片描述
Spring会自动调用@Autowired的方法进行自动注入,在没有调用set的方法的前提下,此时调用get的结果不为null,如下:

package com.lize.demo.dao;import com.lize.demo.TestBean;
import com.lize.demo.TestBean2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component("UserDao")
public class UserDao {private TestBean tb;private TestBean2 tb2;
//    public UserDao(){
//        System.out.println("构造函数");
//    }@Autowiredpublic UserDao(@Autowired(required = false) TestBean tb){System.out.println("有参的构造函数"+tb);this.tb = tb;}@Autowiredpublic void setTb2(TestBean2 tb2){this.tb2 = tb2;}public TestBean2 getTb2(){return tb2;}public UserDao(TestBean tb,TestBean2 tb2){System.out.println("有参的构造函数"+tb+tb2);this.tb = tb;this.tb2 = tb2;}public void printUserDao(){System.out.println("UserDao");}}

在这里插入图片描述

3.3 @Inject和@Resource 进行依赖注入

@Resource优先根据名字进行查找,找不到再根据类型查找。
@inject不能设置required=false属性,另外还需要添加额外的依赖。
推荐使用构造函数进行注入,或者@Resource进行注入

3.4 @Value 进行注入

基本数据类型的注入

package com.lize.demo.entity;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component
public class User {@Value("lize")private String name;@Value("19")private Integer age;@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +", age=" + age +'}';}
}
package com.lize.demo;import com.lize.demo.entity.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class DemoApplicationTests {@Testvoid contextLoads(@Autowired User user) {System.out.println(user);}}

在这里插入图片描述
如果想通过从文件中的数据进行注入,如下,新建a.properties
在这里插入图片描述
在这里插入图片描述
在Spring Boot项目中,如果想获取配置文件application.properties中的数据,不需要使用@PropertySource指定路径文件,如下:
在这里插入图片描述
在这里插入图片描述
如果在数据文件获取不到对应数据,在Spring Boot项目中会报错(解决方法为在变量名后面加入“:”填写默认值),但是在Spring中会指定把值直接注入到对应变量。
在这里插入图片描述
复杂数据类型的注入,使用spel表达式的方式进行注入,如下:

package com.lize.demo.entity;import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;import java.util.List;
import java.util.Map;@Component
public class User {@Value("#{{'语文':'98'}}")private Map<String,String> score;@Value("#{'王者,原神'}")private List<String> like_games;@Overridepublic String toString() {return "User{" +"score=" + score +", like_games=" + like_games +'}';}
}

在这里插入图片描述

http://www.ds6.com.cn/news/102652.html

相关文章:

  • 永嘉移动网站建设公司百度教育网站
  • 游戏开发师北京排名seo
  • 常德做网站公司哪家好seo管理与优化期末试题
  • 乌鲁木齐网站设计平台青岛百度关键词优化
  • wordpress 文章 新窗口企业网站优化外包
  • 网站挂百度推广百度竞价排名推广
  • 网站开发微信登录流程百度搜索风云榜排行榜
  • 口子网站怎么做百度竞价渠道代理
  • 网站建设yu陕西seo推广
  • 有培训做网站 小程序的学校唐老鸭微信营销软件
  • 印度网站建设多少钱十大流量平台
  • 查备案网站备案可以免费打开网站的软件
  • 做地图的网站软文代发布
  • 徐州市网站建设国外免费源码共享网站
  • 音乐网站要怎么做怎样建立网站免费的
  • 网站标题图片怎么做高端企业建站公司
  • 访问behance设计网站热门关键词
  • 青岛网站模板外链平台有哪些
  • 餐饮网站建设怎么建设的色盲测试图
  • 大学做机器人比赛的网站论坛杭州龙席网络seo
  • 网站后台栏目根据什么做的全网搜索软件
  • 成立公司法人有什么风险下载优化大师
  • 温州知名网站推广中国新闻
  • wordpress.enseo网站设计工具
  • 公司网站建设的改进的建议南宁seo外包靠谱吗
  • 做企业展示型网站的好处大二网页设计作业成品
  • h5响应式网站上海安卓优化大师旧版本
  • JAVA网站开发程序员热点事件
  • 入群修改网站后台游戏推广论坛
  • 网站开发签呈如何写站长工具seo词语排名