<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                **1. 兩大類的Bean注解** 下面是常用的Bean注解,Bean注解分成兩大類:用于創建Bean的注解和用于屬性注入的注解。 (1)用于創建Bean的注解 ```java @Component:是所有Spring注解的基石,后面見到的所有Spring注解都是基于該注解開發而來 @Repository:通常用于dao層 @Service:通常用于service層 @Controller:通常用于controller層 ``` (2)用于屬性注入的注解 ``` @Autowired:根據類型自動裝配 @Qualifier:必須與@Autowired一起使用,根據名字自動裝配 @Resource:根據名字,或類型自動裝配 @Value:用于對一個String類型的屬性賦值 ``` <br/> **2. Bean注解的使用步驟** (1)在`resources/ApplicationContext.xml`中開啟包掃描。 ```xml <?xml version='1.0' encoding='UTF-8'?> <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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--開啟包掃描,它可以掃描當前包及其子包;如果需要掃描多個包,可以使用 , 隔開--> <context:component-scan base-package="com.learn.spring06.*"/> </beans> ``` (2)在dao層中使用。 ```java /** * (1)如果是 @Repository ,則會自動創建 <bean id="studentDaoImpl class="xx.dao.impl.StudentDaoImpl"/> * (2)如果是 @Repository("studentDaoImpl2"),則會自動創建 <bean id="studentDaoImpl2 class="xx.dao.impl.StudentDaoImpl"/> * 其中@Component、@Service、@Controller也是一樣的道理 */ @Repository public class StudentDaoImpl implements StudentDao { @Override public void printName(String name) { System.out.println(name); } } ``` (3)在service層中使用。 ```java @Service public class StudentServiceImpl implements StudentService { /** * (1)@@Autowired,根據類型自動裝配,Spring會自動判斷StudentDaoImpl是StudentDao類型,所以StudentDaoImpl將自動裝配給該屬性 * (2)@Qualifier與@Autowired 共同使用,實現按名字自動裝配 */ @Autowired //@Qualifier("studentDaoImpl") private StudentDao studentDao; //@Resource 這樣寫則根據類型自動裝配 //@Resource("studentDaoImpl") 這樣寫則根據名字自動裝配 //private StudentDao studentDao; @Value(value = "張三") //自動將張三賦值給name屬性 private String name; @Override public void printName(String name) { if (name == null) { studentDao.printName(this.name); } else { studentDao.printName(name); } } } ``` (4) 測試。 ```java @Test public void printName() { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); StudentService studentService = context.getBean(StudentService.class); studentService.printName(null); //張三 } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看