欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring實戰(zhàn)之@Autowire注解用法詳解

 更新時間:2019年12月27日 09:16:11   作者:cakincqm  
這篇文章主要介紹了Spring實戰(zhàn)之@Autowire注解用法,結合實例形式詳細分析了Spring @Autowire注解具體實現(xiàn)步驟與相關使用技巧,需要的朋友可以參考下

本文實例講述了Spring實戰(zhàn)之@Autowire注解用法。分享給大家供大家參考,具體如下:

一 配置

<?xml version="1.0" encoding="GBK"?>
<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-4.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-4.0.xsd">
   <context:component-scan
      base-package="org.crazyit.app.service,org.crazyit.app.dao"/>   
</beans>

二 dao接口

BaseDao

package org.crazyit.app.dao;
public interface BaseDao<T>
{
   void save(T e);
}

ItemDao

package org.crazyit.app.dao;
import org.crazyit.app.domain.*;
public interface ItemDao extends BaseDao<Item>
{
}

UserDao

package org.crazyit.app.dao;
import org.crazyit.app.domain.*;
public interface UserDao extends BaseDao<User>
{
}

三 dao實現(xiàn)類

BaseDaoImpl

package org.crazyit.app.dao.impl;
import org.crazyit.app.dao.*;
public class BaseDaoImpl<T> implements BaseDao<T>
{
   public void save(T e)
   {
      System.out.println("程序保存對象:" + e);
   }
}

ItemDaoImpl

package org.crazyit.app.dao.impl;
import org.springframework.stereotype.*;
import org.crazyit.app.dao.*;
import org.crazyit.app.domain.*;
@Component("itemDao")
public class ItemDaoImpl extends BaseDaoImpl<Item>
  implements ItemDao
{
}

UserDaoImpl

package org.crazyit.app.dao.impl;
import org.springframework.stereotype.*;
import org.crazyit.app.dao.*;
import org.crazyit.app.domain.*;
@Component("userDao")
public class UserDaoImpl extends BaseDaoImpl<User>
  implements UserDao
{
}

四 Bean

Item

package org.crazyit.app.domain;
public class Item
{
}

User

package org.crazyit.app.domain;
public class User
{
}

五 service接口

BaseService

package org.crazyit.app.service;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import org.crazyit.app.service.*;
public interface BaseService<T>
{
  void addEntity(T entity);
}

ItemService

package org.crazyit.app.service;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import org.crazyit.app.service.*;
import org.crazyit.app.domain.*;
@Component
public interface ItemService extends BaseService<Item>
{
}

UserService

package org.crazyit.app.service;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import org.crazyit.app.service.*;
import org.crazyit.app.domain.*;
@Component
public interface UserService extends BaseService<User>
{
}

六 Service實現(xiàn)類

BaseServiceImpl

package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import org.crazyit.app.dao.*;
import org.crazyit.app.service.*;
public class BaseServiceImpl<T> implements BaseService<T>
{
  @Autowired
  private BaseDao<T> dao;
  public void addEntity(T entity)
  {
    System.out.println("調用" + dao
      + "保存實體:" + entity);
  }
}

ItemServiceImpl

package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import org.crazyit.app.service.*;
import org.crazyit.app.domain.*;
@Component("itemService")
public class ItemServiceImpl extends BaseServiceImpl<Item>
  implements ItemService
{
}

UserServiceImpl

package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import org.crazyit.app.service.*;
import org.crazyit.app.domain.*;
@Component("userService")
public class UserServiceImpl extends BaseServiceImpl<User>
  implements UserService
{
}

七 測試類

package lee;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.crazyit.app.service.*;
import org.crazyit.app.domain.*;
public class BeanTest
{
  public static void main(String[] args)throws Exception
  {
    // 創(chuàng)建Spring容器
    ApplicationContext ctx = new
      ClassPathXmlApplicationContext("beans.xml");
    UserService us = ctx.getBean("userService", UserService.class);
    us.addEntity(new User());
    ItemService is = ctx.getBean("itemService", ItemService.class);
    is.addEntity(new Item());
  }
}

八 測試

調用org.crazyit.app.dao.impl.UserDaoImpl@b7dd107保存實體:org.crazyit.app.domain.User@42eca56e調用org.crazyit.app.dao.impl.ItemDaoImpl@52f759d7保存實體:org.crazyit.app.domain.Item@7cbd213e

更多關于java相關內容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數(shù)據(jù)結構與算法教程》、《Java操作DOM節(jié)點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》

希望本文所述對大家java程序設計有所幫助。

相關文章

  • Spring?Cloud?OAuth2實現(xiàn)自定義token返回格式

    Spring?Cloud?OAuth2實現(xiàn)自定義token返回格式

    Spring?Security?OAuth的token返回格式都是默認的,但是往往這個格式是不適配系統(tǒng)。本文將用一個接口優(yōu)雅的實現(xiàn)?Spring?Cloud?OAuth2?自定義token返回格式,需要的可以參考一下
    2022-06-06
  • Java中管理資源的引用隊列相關原理解析

    Java中管理資源的引用隊列相關原理解析

    這篇文章主要介紹了Java中管理資源的引用隊列相關原理解析,涉及到Java的垃圾回收機制方面的知識,需要的朋友可以參考下
    2015-12-12
  • 一篇文章帶你了解spring事務失效的多種場景

    一篇文章帶你了解spring事務失效的多種場景

    在日常編碼過程中常常涉及到事務,在前兩天看到一篇文章提到了Spring事務,那么在此總結下在Spring環(huán)境下事務失效的幾種原因.
    2021-09-09
  • Java實現(xiàn)鼠標隨機移動效果的示例代碼

    Java實現(xiàn)鼠標隨機移動效果的示例代碼

    有的時候我們需要鼠標一直滑動的情況,為了節(jié)省時間,本文用Java語言寫了一個腳本,可以實現(xiàn)鼠標隨機移動,感興趣的小伙伴可以了解一下
    2022-05-05
  • 淺談SpringCloud的微服務架構組件

    淺談SpringCloud的微服務架構組件

    這篇文章主要介紹了淺談SpringCloud的微服務架構組件,Spring Cloud根據(jù)分布式服務協(xié)調治理的需求成立了許多子項目,每個項目通過特定的組件去實現(xiàn),需要的朋友可以參考下
    2023-04-04
  • SpringBoot替換默認的tomcat服務器的方法

    SpringBoot替換默認的tomcat服務器的方法

    Tomcat是Apache基金下的一個輕量級的Servlet容器,支持Servlet和JSP,Tomcat具有Web服務器特有的功能,在SpringBoot框架中,我們使用最多的是Tomcat,這是SpringBoot默認的容器技術,本文給大家介紹了Spring?Boot如何替換默認的tomcat服務器,需要的朋友可以參考下
    2024-08-08
  • 利用Java獲取文件名、類名、方法名和行號的方法小結

    利用Java獲取文件名、類名、方法名和行號的方法小結

    這篇文章運用實例代碼給大家介紹了利用Java怎樣獲取文件名、類名、方法名和行號,有需要的可以參考借鑒,下面一起來看看吧。
    2016-08-08
  • 解決RestTemplate第一次請求響應速度較慢的問題

    解決RestTemplate第一次請求響應速度較慢的問題

    這篇文章主要介紹了解決RestTemplate第一次請求響應速度較慢的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • springboot整合JPA過程解析

    springboot整合JPA過程解析

    這篇文章主要介紹了springboot整合JPA過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • Java項目中防止SQL注入的四種方案總結

    Java項目中防止SQL注入的四種方案總結

    SQL注入是一種代碼注入技術,通過把SQL命令插入到Web表單遞交或輸入域名或頁面請求的查詢字符串,最終達到欺騙服務器執(zhí)行惡意的SQL命令,下面我們就來看看如何在項目中防止SQL注入吧
    2023-10-10

最新評論