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

邯郸新闻沈阳seo按天计费

邯郸新闻,沈阳seo按天计费,域名 利用域名做网站 邮箱,喜满堂网站建设目录 一、Bean 的简单使用 1、正常情况 2、问题提出 二、解决方案 1、Qualifier 2、直接写方法名 三、特殊情况 1、DataSource 一、Bean 的简单使用 在开发中,基于 XML 文件配置 Bean 对象的做法非常繁琐且不好维护,因此绝大部分情况下都是使用…

目录

一、@Bean 的简单使用

1、正常情况

2、问题提出

二、解决方案

1、@Qualifier

2、直接写方法名

三、特殊情况

1、DataSource


一、@Bean 的简单使用

在开发中,基于 XML 文件配置 Bean 对象的做法非常繁琐且不好维护,因此绝大部分情况下都是使用“完全注解开发”。

对于 Spring 而言,IOC 容器中的 Bean 对象的创建和使用是一大重点,Spring 也为我们提供了注解方式创建 bean 对象:使用 @Bean。

在举例之前,先熟悉以下两个需要用到的类:

(1)User 类

package com.demo.pojo;import org.springframework.stereotype.Component;@Component
public class User {private String name;public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +'}';}
}

 (2)Book 类

package com.demo.pojo;import org.springframework.stereotype.Component;@Component
public class Book {private User user;public void setUser(User user) {this.user = user;}public User getUser() {return user;}
}

1、正常情况

(1)下面是一个简单的 bean 对象创建:

@Bean
public User getUser() {return new User();
}

经此操作,IOC 中就会多出一个与 <bean id = "getUser" class = "类路径.User"/> 同义的 bean 对象。即:方法名就是 id,返回类型就是 class

(2)含有普通类型参数的 bean 对象创建:

@Bean
public User getUser(@Value("wyt") String name) {User user = new User();user.setName(name);return user;
}

需要注意的是,方法中的参数并不是 bean 对象的成员属性,而是代表着 bean 对象的创建依赖于这几个参数,或许用来 setParameter,或许只是中间变量。

而在 xml 文件中的 <property> 则必须要求用 set 方法。

(3)含有对象类型参数的 bean 对象创建:

package com.demo.config;import com.demo.pojo.Book;
import com.demo.pojo.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;@Configuration
public class Config {@Beanpublic User getUser(@Value("wyt") String name) {User user = new User();user.setName(name);return user;}@Beanpublic Book createBook(User user) {Book book = new Book();book.setUser(user);return book;}
}

对比在 xml 中是如何实现参数是对象类型时的属性注入:

(4)正常情况下,@Bean 使用总结

(4-1)@Bean 注解的方法,其返回值就是一个 <bean> 对象,并且注册到了 IOC 容器中。

  • id 为方法的名字;
  • class 为方法的返回类型;

(4-2)@Bean 对象的实例化依赖于方法内的参数,参数可以是普通类型,也可以是对象类型。

  • 若为普通类型,用 @Value("xxx") 来注入这个参数;
  • 若为对象类型,则默认情况下,Spring 会到 IOC 容器中寻找与参数类型相同的 bean 对象来注入这个参数;

2、问题提出

根据上述内容,我们会想到:如果参数是对象类型,可是 IOC 中拥有不止一个相同类型的 bean 对象,这该怎么办呢?

比如下面的情况:

可以看到,Book 对象的参数 user 已经报错了,无法自动装配,因为存在多个 User 类型的 Bean,Spring 不知道应该将哪个 bean 注入到 user 中

二、解决方案

首先我们要知道,默认情况下,参数的注入使用的是 @Autowired,不需要显式写出。

1、@Qualifier

我们可以想到,@Autowired 是根据类型自动装配,当一个类型有多个 bean 对象时失效。

而 @Qualifier 是根据名称进行装配,这不就意味着我们可以用 @Qualifier 来明确需要的 bean 对象嘛。

这有两种写法,都是有效的:

  • 直接在参数前加 @Qualifier("function_name");
  • 在方法前加 @Qualifier("function_别名"),在参数前加 @Qualifier("function_别名");

(1)配置类

package com.demo.config;import com.demo.pojo.Book;
import com.demo.pojo.User;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;@Configuration
public class Config {@Beanpublic User getUser(@Value("wyt") String name) {User user = new User();user.setName(name);return user;}@Beanpublic User createUser(@Value("wyt") String name) {User user = new User();user.setName(name);return user;}@Beanpublic Book createBook(@Qualifier("createUser") User user) {Book book = new Book();book.setUser(user);return book;}
}

(2)测试代码

@Test
public void SameObjectTest() {ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);Book book = context.getBean("createBook", Book.class);System.out.println(book.getUser());
}

(3)输出结果

2、直接写方法名

因为一般情况下,方法名不能相同,因此也可以通过将参数名写成对应的方法名来进行注入

package com.demo.config;import com.demo.pojo.Book;
import com.demo.pojo.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;@Configuration
public class Config {@Beanpublic User getUser(@Value("wyt") String name) {User user = new User();user.setName(name);return user;}@Beanpublic User createUser(@Value("wyt") String name) {User user = new User();user.setName(name);return user;}@Beanpublic Book createBook(User createUser) {Book book = new Book();book.setUser(createUser);return book;}
}

三、特殊情况

在这里要说明一些特别的对象类型,比如:DataSource。

1、DataSource

(1)问题描述

@Bean
public DruidDataSource createDruidDataSource() {DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName(driver);dataSource.setUrl(url);dataSource.setUsername(username);dataSource.setPassword(password);return dataSource;
}@Bean
public DruidDataSource getDruidDataSource() {DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName(driver);dataSource.setUrl(url);dataSource.setUsername(username);dataSource.setPassword(password);return dataSource;
}

当我们像上面的代码一样,写了两个数据源的 bean 对象之后,运行就会出现如下报错: 

No qualifying bean of type 'javax.sql.DataSource' available: expected single matching bean but found 2

简单来说,就是因为 Spring 有其自身的配置类,导致程序不知道选择哪一个数据源。

(2)解决方法

给其中任意一个加上 @Primary,代表当出现多个同类型 bean 时,优先使用哪一个。写上之后,就可以使用前文所述的方法,如:@Qualifier 或直接写方法名。

@Bean
@Primary
public DruidDataSource createDruidDataSource() {DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName(driver);dataSource.setUrl(url);dataSource.setUsername(username);dataSource.setPassword(password);return dataSource;
}

http://www.mnyf.cn/news/37051.html

相关文章:

  • wordpress亲子主题网络优化工程师需要学什么
  • 网站设计有限公司今日军事新闻头条视频
  • 建设一个网站报价b2b网站排名
  • 外包接单平台网站抖音关键词排名查询
  • 自己做网站可以用私有云吗电脑培训机构
  • 泉州网站排名优化关键词优化排名软件哪家好
  • 个人餐饮网站模板品牌运营公司
  • wordpress栏目列表页郑州seo优化外包公司
  • 网站后台显示连接已重置来宾seo
  • 泉州网站建设学徒招聘广州最新重大新闻
  • 家庭装修公司seo关键词有话要多少钱
  • 南京网站开发推南京乐识互动营销的概念
  • 诸城哪有做公司网站和的企业站seo
  • html5建设摄影网站意义网站seo关键词优化
  • mac做网站改html文件网络推广一般怎么收费
  • joomla 做 企业网站一个网站如何推广
  • 便宜做网站靠谱吗百度搜索量最大的关键词
  • Mac怎么搭建网站开发环境统计工具
  • p2p网站建设方案国际新闻消息
  • 购物网站有哪些模块快速排名官网
  • flash网站cms徐州seo排名收费
  • 找做网站公司需要注意什么如何网络推广新产品
  • 深圳网站建设phpgoogleseo排名公司
  • 外包做网站平台 一分钟百度推广电话客服
  • 沈阳模板建站方案品牌广告文案
  • 火车头采集网站黄山seo排名优化技术
  • 做电玩城设计的网站南宁seo推广公司
  • 做网络课程的网站百度新闻排行榜
  • 开发公司甲供材料管理办法网站优化关键词排名公司
  • 品牌网站建设费公众号推广渠道