Spring 어노테이션

까만호랭·2023년 9월 5일

Spring 클래스 선언에 대한 Annotaion 공부해보기

Component

개발자가 생성한 ClassSpringBean으로 등록할 때 사용하는 Annotation이다.

@Component
public class Jikwon {
	void Cheerup(){
    System.out.println("힘내자");
}

이런 식으로 @Component를 해줌으로써

Componentex componentex = new Componentex();

componentex이라는 객체가 생성되었다.
동시에 빈을 선언한 것이다.

Service

Service는 비즈니스 로직을 구현하기 위한 클래스임을 알려주는 Annotation이다.
(다른 빈에서 가져와 처리를 하고 결과를 제공하는 비즈니스 로직)

@Service
public class BusinessImpl implements BusinessInter{
	@Autowired  
	private JikwonInter jikwonInter;  // Autowired 해줌으로써 클래스의 포함관계가 성립
	
	@Override
	public void dataShow() {
		List<JikwonDto> list = (ArrayList<JikwonDto>)jikwonInter.selectList();
		
		for(JikwonDto s:list) {
			System.out.println(s.getJikwon_no() + " " + s.getJikwon_name() + " " + s.getBuser_name()+ " " + s.getIbsa());
		}
	}
}

Repository

Repository는 DB자료를 처리하는 클래스임을 알리는 Annotation이다.

@Repository
public class SangpumImpl implements SangpumInter{
	private Connection conn;
	private PreparedStatement pstmt;
	private ResultSet rs;
	
	
	public SangpumImpl() {
		try {
			Class.forName("org.mariadb.jdbc.Driver");
		} catch (Exception e) {
			System.out.println("Driver file 로딩 실패 : "+ e);
		}
	}
	
	@Override
	public ArrayList<SangpumDto> selectList() {
		ArrayList<SangpumDto> list = new ArrayList<SangpumDto>();
		try {
			
			conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/test","root", "seoho123");
			String sql = "select * from sangdata";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()) {
				dto.setCode(rs.getString("code"));  
				dto.setSang(rs.getString("sang"));
				dto.setSu(rs.getString("su"));
				dto.setDan(rs.getString("dan"));
				list.add(dto);
			}
			
		} catch (Exception e) {
			System.out.println("getDataAll err : " + e);
		} finally {
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (Exception e2) {
			}
		}
		return list;
	}
}

처럼 DB연동을 처리해주는 클래스임을 알려준다.

Configuration

Configuration가 선언 된 자바 클래스는 스프링 설정을 담당하는 클래스이다.

@Configuration
public class ConfigClass {
	...
}

자바 클래스를 설정파일로 만들어 주었으면 그 안에 객체들을 빈으로 등록해야 한다.

profile
남들과 함께 발자국을 남기는 까만호랭

0개의 댓글