Code Monkey home page Code Monkey logo

k5_springboot's Introduction

Spring IoC와 @Autowired 어노테이션

Spring은 'IoC 컨테이너'로 객체 간의 의존성을 관리한다.
이를 편리하게 처리하기 위해 @Autowired 어노테이션을 제공한다.

public class MemberServiceTest {
    @Autowired
    private MemberService memberService;
}

@Autowired 어노테이션이 MemberServiceTest 클래스의 memberService 멤버 변수에 사용됨.
이는 Spring IoC 컨테이너에서 자동으로 MemberService 타입의 빈을 찾아서 주입하라는 의미.
개발자가 직접 객체를 생성하거나 주입할 필요 없이 Spring이 적절한 빈을 찾아서 주입한다.

빈(Bean) 등록

빈을 등록하기 위해서는 해당 클래스에 다양한 어노테이션 중 하나를 사용한다.

1. @Component

가장 일반적인 어노테이션으로, 어떠한 계층 구조에도 속하지 않는 일반적인 Spring 빈을 나타낸다.
주로 재사용 가능한 컴포넌트에 사용한다.

@Component
public class MyComponent {
    // 클래스 내용
}

2. @Service

비즈니스 로직이나 서비스 계층에 해당하는 빈에 사용한다.
주로 서비스 클래스에서 사용되며, @Component의 특화된 형태로 간주된다.

@Service
public class MyService {
    // 클래스 내용
}

3. @Repository

주로 데이터베이스와 관련된 작업을 수행하는 DAO(Data Access Object) 클래스에 사용한다.
데이터베이스 예외를 Spring의 DataAccessException으로 변환한다.

@Repository
public class MyRepository {
    // 클래스 내용
}

4. @Controller

주로 Spring MVC 웹 애플리케이션에서 컨트롤러 역할을 하는 클래스에 사용한다.
사용자의 요청을 처리하고 적절한 응답을 반환하는데 사용된다.

@Controller
public class MyController {
    // 클래스 내용
}

어노테이션은 코드를 논리적으로 그룹화하고, Spring IoC 컨테이너에서 빈으로 등록되어 관리되도록 돕는다.

k5_springboot's People

Contributors

sonenhur avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.