spring-依赖注入

  HttpClientBuilder httpClientBuilder = HttpClients.custom();
  
  HttpClient httpClient = httpClientBuilder.build();
  
  HttpHost httpHost = new HttpHost("服务器IP地址", 80);
  
  HttpPost httpGet = new HttpPost("URL地址.asmx");
  
  httpGet.setHeader("SOAPAction", "http://tempuri.org/action名称");
  
  httpGet.setHeader("Content-Type", "text/xml; charset=utf-8");
  
  String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  
  str = st;
  
  str = str + "<soap:Body>";
  
  str = str + "<数据标签 xmlns=\"http://名字空间/\" >";
  
  str = str + "各种参数";
  
  str = str + "</数据标签>";
  
  str = str + "</soap:Body>";
  
  str = str + "</soap:Envelope>";
  
  StringEntity entity = new StringEntity(str, Charset.forName("UTF-8"));
  
  entity.setContentEncoding("UTF-8");
  
  entity.setContentType("application/xml");
  
  httpGet.setEntity(entity);
  
  HttpResponse httpResponse = httpClient.execute(httpHost, httpGet);
  
  HttpEntity httpEntity = httpResponse.getEntity();
  
  InputStream inputStream = httpEntity.getContent();
  
  InputStreamReader inputStreamreader = new InputStreamReader(inputStream, "utf-8");
  
  BufferedReader bufferedReader = new BufferedReader(inputStreamreader);
  
  StringBuilder result = new StringBuilder();
  
  while (true) {
  
  char buf[] = new char[1024];
  
  int readed = bufferedReader.read(buf);
  
  if (readed <= 0) {
  
  break;
  
  }
  
  result.append(buf, 0, readed);
  
  }
  
  bufferedReader.close();
  
  System.out.println(result);
  
  、注解@Autowired
  
  配置类:向spring容器注册HelloDao和HelloService1 两个bean实例
  
  @Configuration//告诉spring容器这是一个配置类
  
  public class DiHelloConfig1 {
  
  @Bean
  
  public HelloService1 helloService(){
  
  return new HelloService1();
  
  }
  
  @Bean
  
  public HelloDao helloDao(){
  
  HelloDao helloDao = new HelloDao();
  
  helloDao.setMessge("hello di");
  
  return helloDao;
  
  }
  
  }
  
  HelloDao:该类里定义了getHello方法返回一个字符串。
  
  public class HelloDao {
  
  String messge;
  
  public String getHello(){
  
  return messge;
  
  }
  
  public void setMessge(String messge) {
  
  this.messge = messge;
  
  }
  
  }
  
  HelloService1:该类通过@Autowired注解告诉spring要给该属性注入bean实例,在使用@Autowired时,首先在容器中查询对应类型的bean,相当于ioc.getBean(HelloService1.class)。
  
  public class HelloService1 {
  
  @Autowired
  
  HelloDao helloDao;
  
  public String getHello(){
  
  return helloDao.getHello();
  
  }
  
  }
  
  测试运行
  
  @Test
  
  public void test1(){
  
  //1、传入配置类,新建spring容器对象,spring容器根据配置类的信息,生成bean实例,并注册到spring容器中
  
  AnnotationConfigApplicationContext ioc = new AnnotationConfigApplicationContext(DiHelloConfig1.class);
  
  //从spring容器中获取该对象
  
  HelloService1 helloService1 = ioc.getBean(HelloService1.class);
  
  System.out.println(helloService1.getHello());
  
  }
  
  从结果可以看出helloService1 成功的调用了helloDao1里面的方法,说明spring帮我们给属性注入了值。
  
  2、注解@Qualifier
  
  配置类
  
  在配置类里我们注册了三个bean:其中HelloDao 这种类型的bean,我们注册了两个。一个bean的name为helloDao1,另一个bean的name为helloDao2.
  
  @Configuration//告诉spring容器这是一个配置类
  
  public class DiHelloConfig2 {
  
  @Bean
  
  public HelloService2 helloService2(){
  
  return new HelloService2();
  
  }
  
  @Bean//给容器注册一个bean,可以指定name 比如@Bean(name="helloDao1"),默认name为方法名
  
  public HelloDao helloDao1(){
  
  HelloDao helloDao1 = new HelloDao();
  
  helloDao1.setMessge("hello di 1");
  
  return helloDao1;
  
  }
  
  @Bean//给容器注册一个bean,可以指定name 比如@Bean(name="helloDao2"),默认name为方法名
  
  public HelloDao helloDao2(){
  
  HelloDao helloDao1 = new HelloDao();
  
  helloDao1.setMessge("hello di 2");
  
  return helloDao1;
  
  }
  
  }
  
  HelloService2:该类和上面HelloService1一样,需要依赖HelloDao。
  
  public class HelloService2 {
  
  @Autowired
  
  HelloDao helloDao;
  
  public String getHello(){
  
  return helloDao.getHello();
  
  }
  
  }
  
  运行下面的程序
  
  @Test
  
  public void test2(){
  
  //1、传入配置类,新建spring容器对象,spring容器根据配置类的信息,生成bean实例,并注册到spring容器中
  
  AnnotationConfigApplicationContext ioc = new AnnotationConfigApplicationContext(DiHelloConfig2.class);
  
  //从spring容器中获取该对象
  
  HelloService2 helloService2 = ioc.getBean(HelloService2.class);
  
  System.out.println(helloService2.getHello(www.dasheng178.com));
  
  }
  
  会发现报错,这是什么原因呢?
  
  原因就是使用@Autowired注解,有下面几种情况:
  
  首先根据类型去查询,如果查询结果刚好为一个,就将该bean装配给@Autowired指定的成员变量。
  
  如果查询的结果不止一个,那么@Autowired会根据属性名【比如为helloDao1】来查找,也就是ioc.getBean(“helloDao1”)这种方式。
  
  如果根据属性名找不到,spring会抛出上面面的异常。
  
  如何解决?
  
  修改HelloService2的属性名
  
  public class HelloService2 {
  
  @Autowired
  
  HelloDao helloDao2;
  
  public String getHello(){
  
  return helloDao2.getHello();
  
  }
  
  }
  
  再次运行测试,可以看到,spring成功的给HelloService2注入了name为helloDao2的bean实例。
  
  如果我们不想修改属性名,还有没有办法?
  
  这时就需要@Qualifier这个注解,加上该注解后spring就会根据注解里的“helloDao2”去查找bean实例。
  
  public class HelloService2 {
  
  @Qualifier("helloDao2")
  
  @Autowired
  
  HelloDao helloDao;
  
  public String getHello(){
  
  return helloDao.getHello();
  
  }
  
  }
  
  运行测试 , 可以看到也注入成功了。如果有多个实例,推荐使用这种方式。
  
  3、注解@Resource
  
  除了使用@Autowired去注入bean实例,也可以使用@Resource。
  
  @Resource的装配顺序:
  
  1、@Resource后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配
  
  2、指定了name或者type则根据指定的类型去匹配bean
  
  3、指定了name和type则根据指定的name和type去匹配bean,任何一个不匹配都将报错
  
  配置类
  
  @Configuration//告诉spring容器这是一个配置类
  
  public class DiHelloConfig3 {
  
  @Bean
  
  public HelloService3 helloService2(){
  
  return new HelloService3();
  
  }
  
  @Bean//给容器注册一个bean,可以指定name 比如@Bean(name="helloDao1"),默认name为方法名
  
  public HelloDao helloDao1(){
  
  HelloDao helloDao1 = new HelloDao();
  
  helloDao1.setMessge("hello di 1");
  
  return helloDao1;
  
  }
  
  @Bean//给容器注册一个bean,可以指定name 比如@Bean(name="helloDao2"),默认name为方法名
  
  public HelloDao helloDao2(www.tiaotiaoylzc.com/){
  
  HelloDao helloDao1 = new HelloDao();
  
  helloDao1.setMessge("hello di 2");
  
  return helloDao1;
  
  }
  
  }
  
  HelloService3:该类里通过 @Resource去注入name为helloDao2的实例。
  
  public class HelloService3 {
  
  @Resource(name = "helloDao2")
  
  HelloDao helloDao;
  
  public String getHello(){
  
  return helloDao.getHello();
  
  }
  
  }
  
  测试
  
  @Test
  
  public void test3(){
  
  //1、传入配置类,新建spring容器对象,spring容器根据配置类的信息,生成bean实例,并注册到spring容器中
  
  AnnotationConfigApplicationContext ioc = new AnnotationConfigApplicationContext(DiHelloConfig3.class);
  
  //从spring容器中获取该对象
  
  HelloService3 helloService3 = ioc.getBean(HelloService3.class);
  
  System.out.println(helloService3.getHello( www.michenggw.com));
  
  }
  
  从运行的结果,可以看出bean实例注入成功。
  
  思考:
  
  HelloService 依赖 HelloDao,传统的方式我们必须通过new出HelloDao,然后通过set方式去注入HelloDao实例
  
  public void setHelloDao(HelloDao helloDao) {
  
  this.helloDao = helloDao;
  
  }
  
  或者通过构造函数的方式。
  
  public HelloService1(HelloDao helloDao) {
  
  this.helloDao = helloDao;
  
  }
  
  而用了spring之后,注入属性不需要我们自己去完成,只需要加上对应的注解,spring容器就帮我们完成了属性的注入。
  
  这就是spring中对Ioc容器的补充思想-DI(依赖注入)。

猜你喜欢

转载自www.cnblogs.com/qwangxiao/p/10345180.html