「Spring」アノテーション@Componentを使うサンプル

環境
Spring 5.2.4

構文
アノテーション@Component
@Componentは付与したクラスはSpirngのコンポーネントとして認識され、ApplicationContextに登録されることで、DI対象のクラスとなります。
SpringのDIコンテナにbeanとして登録したいクラスへ付与します。

使用例1

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@Component
public class User {
public int getAge() {
int age = 35;
return age;
}
}
@Component public class User { public int getAge() { int age = 35; return age; } }
@Component
public class User {
   	public int getAge() {
            int age = 35;
            return age;
      }
}

使用例2

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@Component
public class UserComponent {
public void funA() {
System.out.println("study skill become");
}
}
@Component public class UserComponent { public void funA() { System.out.println("study skill become"); } }
@Component
public class UserComponent {

    public void funA() {
        System.out.println("study skill become");
    }

}

 

SpringMVC

Posted by arkgame