「Spring」アノテーション@Componentを使うサンプル
環境
Spring 5.2.4
構文
アノテーション@Component
@Componentは付与したクラスはSpirngのコンポーネントとして認識され、ApplicationContextに登録されることで、DI対象のクラスとなります。
SpringのDIコンテナにbeanとして登録したいクラスへ付与します。
使用例1
@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
@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"); } }